嗨,我正在为一份报告编写SQL查询,该报告将给出从上午8点开始的当天的销售线索数量报告设置为每隔一天发出一次,例如,上午8点报告将显示来自上午7点到8点的线索,上午9点报告将显示从上午7点到9点,上午10点从上午7点到10点,等等,我想知道是否有人可以为我提供时间函数来放入where cluase (大致如下:
T.LeadDate >= DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0) -1)
谢谢
发布于 2012-12-12 04:39:05
这会让你在早上7点以后用LeadDate得到所有的东西。当天的
select * from TABLENAME t where t.LeadDate >= dateadd(hh,7, DATEDIFF(dd, 0, GETDATE()))您还可以使用下面的代码,这对我来说似乎更清楚一些。
select * from TABLENAME t where t.LeadDate >= dateadd(hh,7, CONVERT(datetime,CONVERT(date, getdate())))发布于 2012-12-12 04:45:37
WHERE T.LeadDate >= DATEADD(hh,7,DATEDIFF(D, 0, GETDATE()))不需要将DATEDIFF(D, 0, GETDATE())部件转换回某个时间
发布于 2012-12-12 04:52:42
为了完整起见,考虑到您的规则,这将使您获得每天的计数:
-- Get counts of records per day
-- Anything prior to 8am is considered the prior day.
select cast(dateadd(hh, -8, MyDate) as date) FiscalDate, count(*) as Num
from MyTable
group by cast(dateadd(hh, -8, MyDate) as date)
order by cast(dateadd(hh, -8, MyDate) as date)https://stackoverflow.com/questions/13827923
复制相似问题