SELECT tk_pici,tk_kecheng ,(select count(*) from tb_tongkao where tk_chengji='合格')
FROM tb_tongkao
group by tk_pici,tk_kecheng;
按照批次,课程分组统计数据,怎么实现想要的效果
数据源
上面代码查询结果
想要的结果
怎么写sql代码啊啊啊啊啊
------解决思路----------------------
select
tk_pici,
tk_kechen,
sum(case when tk_chengji='合格'then 1 else 0 end) as tj
from #TEMP
group by pici,kechen
------解决思路----------------------
SELECT tk_pici,tk_kecheng
,(select count(*) from tb_tongkao AS T2
WHERE T1.tk_pici=T2.tk_pici
AND T1.tk_kecheng=T2.tk_kecheng
AND tk_chengji='合格')
FROM tb_tongkao AS T1
你的子查询少了个关键的东西