日期:2014-05-17  浏览次数:20555 次

请教多表查询结果按条件求和?
SQL code

select a.piaolei,a.renshu,b.mingcheng as pl 
from Tbl_checkIn a,tbl_PiaoLei b
where a.piaolei=b.bianhao 
and convert(char(10),CheckInTime,120) >= '2012-08-16' 
and convert(char(10),CheckInTime,120) <= '2012-08-16' 
and CheckInNo='01' 


想把查询出来的结果根据条件b.mingcheng='北京'、b.mingcheng='上海',求出a.renshu的总和各是多少,请问应该怎么写?谢谢!

------解决方案--------------------
SQL code
select sum(case when b.mingcheng='北京' then a.renshu else 0 end) '北京人数',sum(case when b.mingcheng='上海' then a.renshu else 0 end) '上海人数'
from Tbl_checkIn a,tbl_PiaoLei b
where a.piaolei=b.bianhao 
and convert(char(10),CheckInTime,120) >= '2012-08-16' 
and convert(char(10),CheckInTime,120) <= '2012-08-16' 
and CheckInNo='01'