论坛首页 综合技术论坛

DB2中多个count语句合并。

浏览 1955 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-02-12  

 

select count(*) from db1 where grade = 1;
select count(*) from db1 where grade = 2;
select count(*) from db1 where grade = 3;

 

如何将这3条sql语句合成一条?

在mysql中:

       SELECT (SELECT COUNT(*) FROM db1 WHERE gread = 1) AS g1,(SELECT COUNT(*) FROM db1WHERE gread = 2) AS g2;

DB2呢?怎么实现?

   发表时间:2014-02-18  
select sum(case when grade=1 then 1 else 0 end) as g1,
sum(case when grade=2 then 1 else 0 end) as g2,
sum(case when grade=3 then 1 else 0 end) as g3 from db1
where grade in (1,2,3)
0 请登录后投票
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics