我陷入尝试在sql查询中进行计算的问题。我有如下表的出席人数:
roll | sub_id | status
abc | 1 | 1
abc | 1 | 0
abc | 2 | 1
xcv | 1 | 1
abc | 2 | 1
abc | 1 | 1
lkj | 2 | 0
这是我的餐桌主题的一个示例:
id | name
1 | Data Structure
2 | Cloud Computing
我想为特定的胶卷选择不同的sub_id,然后用0和1计数status的数量,并链接到主题表并显示其名称。我想要这样的东西:
roll | sub_id | name | status with 0 | status with 1
abc | 1 |Data Structure | 1 | 2
abc | 2 |Cloud Computing | 0 | 2
有人可以向我解释吗?如何处理查询?
你可以做:
SELECT
a.roll,
a.sub_id,
b.name,
SUM(Case when status=0 then 1 else 0 end) as 'status with 0',
SUM(Case when status=1 then 1 else 0 end) as 'status with 1'
FROM
myTable a inner join subject b on
a.sub_id = b.id
group by a.roll, a.sub_id;
我为您做了一个小提琴:http ://sqlfiddle.com/#!9/ 23d1d9/11/0
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句