# ：求一条SQL语句！

www.myexceptions.net  网友分享于：2013-02-20  浏览：4次

wlh, fyje, dxje ， fyrq, dxrq

------解决方案--------------------

------解决方案--------------------

select distinct Sum(dxje) as sumtotal,
(select Sum(dxje)
from fy
where datediff(dd, fyrq, dxrq) between 1 and 30 ) as '1-30',
(select Sum(dxje)
from fy
where datediff(dd, fyrq, dxrq) between 31 and 60 ) as '31-60' ,
(select Sum(dxje)
from fy
where datediff(dd, fyrq, dxrq) >60 ) as '>60'
from fy

------解决方案--------------------

select distinct wlh,Sum(fyje) as sumtotal,
(select Sum(dxje)
from fy
where wlh = a.wlh and datediff(dd, fyrq, dxrq) between 1 and 30) as '1-30 ',
(select Sum(dxje)
from fy
where wlh = a.wlh and datediff(dd, fyrq, dxrq) between 31 and 60) as '31-60 ',
(select Sum(dxje)
from fy
where wlh = a.wlh and datediff(dd, fyrq, dxrq) >60) as ' >60 '
from fy a
group by Wlh
------解决方案--------------------
select wlh,
sum(case when datediff(dd,fyrq,dxrq)<=1 then 0 when datediff(dd,fyrq,dxrq)>=30 then o else fyje end) as '30天内的兑现金额',
sum(case when datediff(dd,fyrq,dxrq)<=30 then 0 when datediff(dd,fyrq,dxrq)>=60 then o else fyje end) as '30天到60天内的兑现金额',
sum(case when datediff(dd,fyrq,dxrq)>=60 then 0 else fyje end) as '60天内的兑现金额'
from ty
group by wlh
order by wlh