我希望在sql语句
中的case语句中进行比较 SUM(CASE
WHEN bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=1
THEN (price * revenueShareAmountLocal * o.mfactor *0.08)
,第二个是
SUM(CASE
WHEN bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=2
THEN (price * revenueShareAmountLocal * o.mfactor *0.04)
我想进行if bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=2 > bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=1
的比较
然后(price * revenueShareAmountLocal * o.mfactor *0.01)
否则
if bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=2 < bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=1
然后(price * revenueShareAmountLocal * o.mfactor *0.08)
简单来说,我需要在案例陈述中进行比较,如果一个大于另一个,那么将其他总和加上其他东西。
答案 0 :(得分:0)
由于您未包含数据或表格结构,我们在此处提供的帮助有限,但听起来您需要以下其中一项:
SUM(CASE
WHEN bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=1
THEN (price * revenueShareAmountLocal * o.mfactor *0.08)
WHEN bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=2
THEN (price * revenueShareAmountLocal * o.mfactor *0.04)
ELSE 0
END
)
或者更加冗长的选项,这表明您应该查看错误的查询设计:
CASE
WHEN SUM(CASE
WHEN bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=2
THEN (price * revenueShareAmountLocal * o.mfactor *0.04)
ELSE 0
END
)
> SUM(CASE
WHEN bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=1
THEN (price * revenueShareAmountLocal * o.mfactor *0.08)
ELSE 0
END
)
THEN SUM(CASE
WHEN bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=2
THEN (price * revenueShareAmountLocal * o.mfactor *0.04)
ELSE 0
END
)
ELSE SUM(CASE
WHEN bt.idBillingStatus = 2 and o.idOperator = 5 and cs.idClub=1
THEN (price * revenueShareAmountLocal * o.mfactor *0.08)
ELSE 0
END
)
END
答案 1 :(得分:0)
您可以在子查询中计算2个总和 然后比较外部查询中的结果。 这可能是最易读的方法。
或者你可以在总和之间加上一个案例。
例如:
-- Using a single table variable for testing reasons
declare @Test table (groupid int, idBillingStatus int, idOperator int, idClub int, price float, revenueShareAmountLocal float, mfactor int);
insert into @Test (groupid, idBillingStatus, idOperator, idClub, price, revenueShareAmountLocal, mfactor) values
(1000,2,5,1,100,20,2),
(1000,2,5,2,100,20,2),
(2000,2,5,2,100,20,2),
(2000,2,5,1,200,20,2),
(2000,2,5,2,100,20,2);
select groupid, idBillingStatus, idOperator, result1, result2,
(case
when result2 > result1 then result2 * 0.01
else result2 * 0.08
end) as finalresult2
from (
select groupid, idBillingStatus, idOperator,
SUM(
case
when idClub = 1 and idBillingStatus = 2 and idOperator = 5
then price * revenueShareAmountLocal * mfactor
end) as result1,
SUM(
case
when idClub = 2 and idBillingStatus = 2 and idOperator = 5
then price * revenueShareAmountLocal * mfactor
end) as result2
from @Test
group by groupid, idBillingStatus, idOperator
) q;
-- On in 1 select, comparing the sums
select groupid, idBillingStatus, idOperator,
CASE
WHEN SUM(case when idClub = 2 and idBillingStatus = 2 and idOperator = 5 then price * revenueShareAmountLocal * mfactor end) >
SUM(case when idClub = 1 and idBillingStatus = 2 and idOperator = 5 then price * revenueShareAmountLocal * mfactor end)
THEN SUM(case when idClub = 2 and idBillingStatus = 2 and idOperator = 5 then price * revenueShareAmountLocal * mfactor end) * 0.01
ELSE SUM(case when idClub = 2 and idBillingStatus = 2 and idOperator = 5 then price * revenueShareAmountLocal * mfactor end) * 0.08
END as result
from @Test
group by groupid, idBillingStatus, idOperator;