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

标签 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
<小时/>

总体查询是这样的。成功计费点击的结果应该等于timebilled

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

成功计费点击次数远远大于结果中的计费时间

最佳答案

不要使用 COUNT,而是使用 SUM,因为 count 也会对空格或 Null 进行计数

  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

关于mysql - SQL 中的 CASE 语句给出不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44175787/

相关文章:

sql - MySQL使用计算返回值来计算其他值

mysql - 如何刷新填充有来自 MYSQL 数据库的数据的 Excel 表

database - Grails 2.5.0-重置数据库以使用测试数据

mysql - (初学者)SQL查询无缘无故抛出错误

php - 通过 'for' 循环插入 MySQL 表,并在字符串之间使用变量

PHP、MySQl 查询未运行

php - 检查mysql中是否存在用户id

SQL:从多个表中合并 Select count(*)

java - 通过从 Scala/Java 编写/执行 SQL 脚本来处理间歇性数据库连接

php - 了解数据库表的 JSON 输出