select userid, sum(CASE ActualPay WHEN cast(ActualPay as int)<0 THEN 0 ELSE ShouldPay END)-sum(CASE ActualPay WHEN cast(ActualPay as int)<0 THEN 0 ELSE ActualPay END) as overdraft from xx group by userid
貌似不起作用
------解决方案--------------------------------------------------------
sum(CASE WHEN 0+ActualPay<0 THEN 0 ELSE ShouldPay end)
------解决方案--------------------------------------------------------
sql server的写法,你上面按个多了点东西:
SELECT userid ,
SUM(CASE WHEN CAST(ActualPay AS INT) < 0 THEN 0
ELSE ShouldPay
END) - SUM(CASE WHEN CAST(ActualPay AS INT) < 0 THEN 0
ELSE ActualPay
END) AS overdraft
FROM xx
GROUP BY userid