求SQL,根据条件统计信息

沙伽more 2014-07-15 10:31:45
表A的结构是:id, articleID, typeID
我想实现的是能不能用一条SQL直接把当typeID=1、2、3时分别统计出来
要这样:select count(typeID=1) as down,t count(typeID=2) as read,t count(typeID=3) as show from a group by articleID 这种写法是错的,我的意思就是想这样,不知道各位大神们觉得能实现否?
...全文
117 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chen357313771 2014-07-15
  • 打赏
  • 举报
回复
引用 2 楼 chen357313771 的回复:
 select count(CASE WHEN typeID=1 THEN 1 ELSE 0 END)  as down, count(CASE WHEN typeID=2 THEN 1 ELSE 0 END)  as read, count(CASE WHEN typeID=3 THEN 1 ELSE 0 END)  as show from a group by articleID  
额。。。错了。。select count(CASE WHEN typeID=1 THEN 1 ELSE null END) as down, count(CASE WHEN typeID=2 THEN 1 ELSE null END) as read, count(CASE WHEN typeID=3 THEN 1 ELSE null END) as show from a group by articleID -- select SUM(CASE WHEN typeID=1 THEN 1 ELSE 0 END) as down , SUM(CASE WHEN typeID=2 THEN 1 ELSE 0 END) as READ , SUM(CASE WHEN typeID=3 THEN 1 ELSE 0 END) as show from a group by articleID
向东流 2014-07-15
  • 打赏
  • 举报
回复
select sum(case typeid when 1 then 1 else 0 end) down, sum(case typeid when 2 then 1 else 0 end) read, sum(case typeid when 3 then 1 else 0 end) show, from a
霜寒月冷 2014-07-15
  • 打赏
  • 举报
回复
select articleID,
max(case when  typeID=1  then count (typeID)else 0 end ) as [down],
max(case when  typeID=2 then count (typeID) else 0 end)  as [read],
max(case when  typeID=3 then count (typeID) else 0 end) as  [show]
 from a group by articleID
chen357313771 2014-07-15
  • 打赏
  • 举报
回复
 select count(CASE WHEN typeID=1 THEN 1 ELSE 0 END)  as down, count(CASE WHEN typeID=2 THEN 1 ELSE 0 END)  as read, count(CASE WHEN typeID=3 THEN 1 ELSE 0 END)  as show from a group by articleID  
哥眼神纯洁不 2014-07-15
  • 打赏
  • 举报
回复
select articleid, count(case when typeID=1 then 1 else null end) as down, count(case when typeID=2 then 1 else null end) as read, count(case when typeID=3 then 1 else null end) as show from a group by articleID

34,575

社区成员

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

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