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

请教多表查询结果按日期求和?
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 CheckInTime>= '2012-10-01' 
and CheckInTime<= '2012-10-07' 
and CheckInNo='01' 



请教各位老师,这个是查询几天的合计人数,如何把查询出来的结果每天显示北京多少人,上海多少人呢?请问应该怎么写?谢谢!

------解决方案--------------------
SQL code
select CheckInTime,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 CheckInTime>= '2012-10-01' 
 and CheckInTime<= '2012-10-07' 
 and CheckInNo='01' 
 GROUP BY CheckInTime