这是有问题的表格,
我想要做的是获得每个人按类型隔离的条目数(claim_by)。例如,
约翰:总条目:4,类型1条目:1,类型2条目:3。
Jane:总条目数:4,类型1条目:4,类型2条目:0。
这就是我现在所拥有的,
SELECT count(id) as total_entry,claimed_by,
(select count(id) from tblIssues where type=1) as type1_entry,
(select count(id) from tblIssues where type=2) as type2_entry
FROM tblIssues
GROUP BY claimed_by
order by count(id) desc
这为total_entry返回正确的值,但对于type1和type2不正确。我认为这是因为我没有在子查询中执行group_by,但我不确定如何正确地执行此操作。
答案 0 :(得分:1)
您可以在sum()
中添加条件。它总计了多少次(1
)。
SELECT claimed_by,
count(id) as total_entry,
sum(type = 1) as type1_entry,
sum(type = 2) as type2_entry,
sum(type = 3) as type3_entry
FROM tblIssues
GROUP BY claimed_by
order by count(id) desc