这是有问题的表格,
我想做的是获取每个人(claimed_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 但我不确定如何正确执行此操作。
最佳答案
您可以在 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
关于android - 子查询中的分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32904924/