Mysql SQL中的CASE语句给出了不正确的值
在sql代码中运行此部分时,我的值异常。SQL语法方面,这一切都好吗Mysql SQL中的CASE语句给出了不正确的值,mysql,sql,database,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)
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比结果中的TimeBilled大得多,而不是COUNTuse 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
您可以将WHERE子句与这些聚合函数一起使用,而不是使用CASE,例如:
SELECT COUNT(*) as `successfulbillinghits`, SUM(price) as `old_revenue`
FROM table bt
WHERE bt.idBillingStatus = 2;
查询中的“bt”表示什么?描述异常-您得到的值是预期值的整数倍吗?如果是这样,那么几乎可以肯定的是,问题出在未显示的受约束联接上us@hammy你能添加当前和预期的输出吗?更新了同样的问题。那么,忽略逻辑和所有,案例陈述本身是好的吗?有别的办法吗?是的,案例陈述很好。有很多选择。您可以使用内部查询,分别计算2个值,然后在外部查询中获取它们query@hammy-目前,关键的细节,如你正在尝试做什么,你期望的结果是什么,你收到的结果是什么,等等,只在你的头脑中。当我们不知道目标是什么,也不知道当前代码为什么达不到目标时,您怎么能期望我们提出备选方案?请尝试:-按cs.idCustomerSubscription分组,cs.msisdn,pro.name,c.name,c.idClub,o.name,o.I运营商,公司名称,cs.subscribeddate,cs.unsubscribeddate,cs.firstBillingDate,cs.lastBilledDate,cs.lastAttemptDate,curr.idcurreny_symbol,curr.symbol,datebt.creationDate,cs.lastBilledAmount,cs.TimeBilled,PriceThreaded。我没打通。这是分组的问题吗?