日期:2014-05-18  浏览次数:20533 次

求正确写法!!
left join (select hh,sum(sl) as sl from SUBFHD where kdrq between GETDATE()-30 and GETDATE() group by hh) xl on a.hh=xl.hh

在a表有一个字段kdrq为单据生成的时间,想把上句中 where 的条件改写成 用a表中的kdrq-30 查询出数据,请问怎么写?


在线等

------解决方案--------------------
你是直接查询,还是要用A表来左连接?

如果是直接查询,如下即可:
select hh,sum(sl) as sl from SUBFHD where kdrq between GETDATE()-30 and GETDATE() group by hh

--如果是想用左连接,你的写法没错.
select a.* , isnull(xl.sl,0) sl from (select distinct hh from subfhd) a 
left join
(select hh,sum(sl) as sl from SUBFHD where kdrq between GETDATE()-30 and GETDATE() group by hh
) xl on a.hh = xl.hh
------解决方案--------------------
left join s1 on kdrq between GETDATE()-30 and GETDATE() 
group by hh


不行?

楼主的写法有些纠结