日期:2014-05-19  浏览次数:20496 次

如何合并这三句sql,使得能够一次性查出来
select   count(declaretype)   专项任务   from   T_DeclareItem   where   stateid=2   group   by   declaretype
having   declaretype= '专项任务 '
select   count(declaretype)   专项任务   from   T_DeclareItem   where   stateid=2   group   by   declaretype
having   declaretype= '自筹经费 '
select   count(declaretype)   专项任务   from   T_DeclareItem   where   stateid=2   group   by   declaretype
having   declaretype= '重点项目 '

------解决方案--------------------
select sum(case when declaretype= '专项任务 ' then 1 else 0 end) as '专项任务 ',
sum(case when declaretype= '自筹经费 ' then 1 else 0 end) as '自筹经费 ',
sum(case when declaretype= '重点项目 ' then 1 else 0 end) as '重点项目 '
from T_DeclareItem where stateid=2 group by declaretype
------解决方案--------------------
select
'专项任务 ' = sum(case when declaretype= '专项任务 ' then 1 else 0 end),
'自筹经费 ' = sum(case when declaretype= '自筹经费 ' then 1 else 0 end),
'重点项目 ' = sum(case when declaretype= '重点项目 ' then 1 else 0 end)
from T_DeclareItem where stateid=2 group by declaretype