2017-05-25 34 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;