一个分组的问题,求教,急

klj218 2007-07-12 10:25:34
小弟要实现的效果就是分类汇总该编号成功和失败的总数,这是我的错误代码,求教
select id, (select count(check1) as check1 from tbl_a where check1='pass' and check2='pass') pass ,( select count(check2) as check2 from tbl_a where check2='failed' and check2='failed') failed from tbl_a group by id, order by id desc

要实现的结果
id pass failed
4 5 0
3 2 3

表中的记录
id check1 check2
4 pass pass
4 pass pass
4 pass pass
4 pass pass
4 pass pass
3 pass pass
3 pass pass
3 failed failed
3 failed failed
3 failed failed
...全文
114 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
大宇_ 2007-07-12
  • 打赏
  • 举报
回复
select id, sum(case when check1='pass' and check2='pass' then 1 else 0 end) as pass ,sum(case when check1='failed' and check2='failed' then 1 else 0 end) as failed from tbl_a group by id order by id desc
paoluo 2007-07-12
  • 打赏
  • 举报
回复
唉,一樣的語句,咋就沒有Haiwer(海阔天空) 的值錢,還比他先貼出來。 :)
klj218 2007-07-12
  • 打赏
  • 举报
回复
学习,结帖
echiynn 2007-07-12
  • 打赏
  • 举报
回复
嘿嘿,沒看清...
昵称被占用了 2007-07-12
  • 打赏
  • 举报
回复
check1='pass' and check2='pass' 的算pass
check1='failed' and check2='failed' 的算failed
还有其他情况就没在统计范围了


昵称被占用了 2007-07-12
  • 打赏
  • 举报
回复
select id, sum(case when check1='pass' and check2='pass' then 1 else 0 end) as pass ,sum(case when check1='failed' and check2='failed' then 1 else 0 end) as failed from tbl_a group by id order by id desc




paoluo 2007-07-12
  • 打赏
  • 举报
回复
應該是兩個條件都滿足才統計。

echiynn(寶琲),你的還是寫少了條件。 :)
paoluo 2007-07-12
  • 打赏
  • 举报
回复
Select
id,
SUM(Case When check1 ='pass' and check2='pass' Then 1 Else 0 End) As pass,
SUM(Case When check1 ='failed' and check2='failed' Then 1 Else 0 End) As failed
From
tbl_a
group by
id
order by
id desc
echiynn 2007-07-12
  • 打赏
  • 举报
回复
錯了...

---------

select id, sum(case check1 when 'pass' then 1 else 0 end) as pass
sum(case check2 when 'failed' then 1 else 0 end) as failed
from table
group by id
echiynn 2007-07-12
  • 打赏
  • 举报
回复
select id, sum(case check1 when 'pass' then 1 else 0 end) as pass
sum(case check2 when 'pass' then 1 else 0 end) as failed
from table
group by id

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧