android - 子查询中的分组依据

标签 android mysql sqlite

这是有问题的表格,

enter image description here

我想做的是获取每个人(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/

相关文章:

Python + sqlite3 : executemany with "constructed" values

mysql - 将mysql数据库复制到sqlite数据库

Android 兼容性库。最佳实践

php - "Your PHP installation appears to be missing the MySQL extension which is required by WordPress."

android - Dagger 2 构造函数注入(inject)等

mysql - 几个名字的列表,用相同的信息更新他们在表中的行

Java: Antlr4 MySql 获取单个语句

ios - 带主键的sqlite插入

带有图标的Android寻呼机滑动选项卡

java - 在有效用户的情况下执行错误的循环