2017-05-25 29 views
0

我在我的sql代码中运行此部分时出现异常值。 SQL语法明智,这一切都可以吗?SQL中的CASE语句给出了不正确的值

select 
    COUNT(CASE WHEN bt.idBillingStatus = 2 
    THEN 1 
     ELSE NULL END)   AS successfulbillinghits, 
    SUM(CASE WHEN bt.idBillingStatus = 2 
    THEN price 
     ELSE 0.0 END) 
        AS old_revenue  
from table 

整个查询是这样的。 successfulbillinghits的结果应该等于timesbilled

SELECT 
    cs.idCustomerSubscription, 
    cs.msisdn, 
    pro.name      AS promoterName, 
    c.name       AS ClubName, 
    c.idClub      AS ClubID, 
    o.name      AS operatorName, 
    o.idOperator     AS OperatorID, 
    co.name      AS country, 
-- cu.customerSince    AS CustomerSince, 
    cs.subscribeddate    AS subscribeddate, 
-- cs.subscriptionNotificationSent AS SubNotificationSent, 
-- cs.eventId      AS EventId, 
    cs.unsubscribeddate   AS unsubscribeddate, 
    cs.firstBillingDate   AS FirstBillingDate, 
    cs.lastBilledDate    As LastBilledDate, 
    cs.lastAttemptDate    AS LastAttemptDate, 
    -- smp.code      AS packageName, 
    -- o.mfactor      AS mmfactor, 
    -- cs.idSubscriptionSource  AS SubscriptionChannel, 
    -- cs.idUnsubscribeSource   AS UnsubscriptionChannel, 
    -- DATE(bt.creationDate)   AS BillingCreationDate, 
    -- bt.price      AS pricePerBilling, 
    -- cs.lastRetryDate    As LastRetryDate, 
    -- cs.lastRenewalDate    AS LastRenewalDate, 
    -- cs.isActive     AS ActiveStatus, 
    -- COUNT(bt.idBillingTransaction) AS BillingAttempts, 
    curr.idcurreny_symbol   AS CurrencyID, 
    curr.symbol     AS currency, 
    date(bt.creationDate)   AS BillingDate, 
    cs.lastBilledAmount   As LastBilledAmount, 
    cs.timesbilled, 
    price, 
-- sum(price), 
    -- revenueShareAmountLocal, 
    -- o.mfactor, 
    -- count(IFF (bt.idBillingStatus = 2,1,0)) as otherversion, 

    count(CASE WHEN bt.idBillingStatus = 2 
    THEN 1 
     ELSE 0 END)   AS successfulbillinghits, 
    SUM(CASE WHEN bt.idBillingStatus = 2 
    THEN price 
     ELSE 0.0 END) 
        AS old_revenue 

FROM 
    customersubscription cs 
    LEFT JOIN 
    billing_transaction bt 
    ON CONVERT(cs.msisdn USING latin1) = bt.msisdn 
     AND cs.idClub = bt.idClub 
     AND bt.creationDate BETWEEN cs.SubscribedDate AND COALESCE(cs.UnsubscribedDate, now()) 

    INNER JOIN customer cu ON (cs.idCustomer = cu.idcustomer) 
    INNER JOIN operator o ON (o.idoperator = cu.idoperator) 
    INNER JOIN country co ON (co.`idCountry` = o.idCountry) 
    INNER JOIN curreny_symbol curr ON (curr.idcurreny_symbol = co.idCurrencySymbol) 
    LEFT JOIN Promoter pro ON cs.idPromoter = pro.id 
    INNER JOIN club_operator_relationships cor ON cor.clubId = cs.idClub 
    INNER JOIN club c ON c.idClub = cs.idClub 
    -- INNER JOIN operator op ON op.idOperator = cu.idOperator 


WHERE 
-- (cs.timesbilled > 0 and cs.subscribeddate < '2016-09-01 00:00:00') 
cs.subscribeddate between '2017-04-20 00:00:00' and '2017-04-21 00:00:00' 
    AND cs.idClub IN (39) 
GROUP BY idCustomerSubscription, ClubName, operatorName, promoterName 

Successfulbillinghits比结果

+0

查询中的'bt'表示什么意思? –

+0

描述“异常” - 你得到的值是预期值的整数倍吗?如果是这样,这个问题几乎可以肯定地是一个受限制的'加入'(你没有给我们看) –

+0

@hammy你可以添加当前和预期的输出吗? –

回答

1

代替COUNT使用SUM timesbilled大得多,因为count计数空白或空也

select 
    SUM(CASE WHEN bt.idBillingStatus = 2 
    THEN 1 
     ELSE 0 END)   AS successfulbillinghits, 
    SUM(CASE WHEN bt.idBillingStatus = 2 
    THEN price 
     ELSE 0.0 END) 
        AS old_revenue  
from table 
+0

同样的问题。所以,忽略逻辑和所有,案件陈述它自我很好?是否有替代方案? – hammy

+0

是的,CASE声明很好。很多替代品存在。您可以使用内部查询并分别计算2个值,然后在外部查询 –

+0

@hammy中获取它们 - 此时,关键详细信息(如*您要做什么*),您期望的结果,您收到的结果,等等,都只在你的脑海中。如果我们不知道目标是什么,或者为什么当前的代码不符合要求,您怎么能期望我们提出替代方案? –

0

相反使用CASE时,可以使用WHERE子句与这些聚合函数,例如:

SELECT COUNT(*) as `successfulbillinghits`, SUM(price) as `old_revenue` 
FROM table bt 
WHERE bt.idBillingStatus = 2;