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

请教多表查询结果求和?
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' 
and left(e.mingcheng,3)='北京市'



想直接把查询出来的结果根据a.renshu只要求出总和就可以,请问应该怎么写?谢谢!

------解决方案--------------------
SQL code


select sum(a.renshu) as renshu
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' 
and left(e.mingcheng,3)='北京市'

------解决方案--------------------
SQL code
select
 renshu,count(1) 
from
(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' 
and left(e.mingcheng,3)='北京市'
)t
group by
  人数