我有下表
# amount type
1 10 0
1 10 0
1 5 1
1 5 1
2 10 0
2 10 0
2 5 1
2 5 10表示现金,1表示类型列中的信用。
问题是要找出现金使用和信贷使用的总数以及每个ID的总金额。
我在寻找得到以下结果的查询
# cash credit total
1 20 10 30
2 20 10 30如果可能的话,我想使用一个查询
谢谢
发布于 2013-06-23 12:04:44
SELECT id,
SUM(CASE WHEN type = 0 THEN amount ELSE 0 END) as "cash",
SUM(CASE WHEN type = 1 THEN amount ELSE 0 END) as "credit",
SUM(amount) as "total"
FROM your_table
GROUP BY id发布于 2013-06-23 12:05:00
SELECT
num,
SUM(CASE WHEN type=0 THEN amount END) cash,
SUM(CASE WHEN type=1 THEN amount END) credit,
SUM(amount) total
FROM
yourtable
GROUP BY num发布于 2013-06-23 12:07:43
select id,
sum(amount) as "total",
CASE WHEN type = 0 THEN amount ELSE 0 END as "cash",
CASE WHEN type = 1 THEN amount ELSE 0 END as "credit",
from table
group by idhttps://stackoverflow.com/questions/17260548
复制相似问题