SQL中的CASE语句给出的值不正确

时间:2017-05-25 08:25:13

标签: mysql sql database

我在sql代码中运行此部分时出现异常值。 SQL语法明智,一切都好吗?

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

总体质疑是这样的。成功开票的结果应该等于时间戳

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远远超过结果中的次数

2 个答案:

答案 0 :(得分:1)

而不是COUNT使用SUM,因为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

答案 1 :(得分:0)

您可以将CASE子句与这些聚合函数一起使用,而不是使用WHERE,例如:

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