1

嗨,我正在为一份报告编写 SQL 查询,该报告将提供自上午 8 点以来当天进入的潜在客户数量。该报告将在我们的每个人中发布,例如,上午 8 点报告将显示从上午 7 点到 8 点进入的潜在客户,上午 9 点报告将显示从上午 7 点到上午 9 点,上午 10 点从上午 7 点到上午 10 点等等,我想知道如果有人可以为我提供时间函数以放入wherecluase(类似于:

T.LeadDate >= DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0) -1)

谢谢

4

3 回答 3

1

这将在早上 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-11T20:39:04.507 回答
1
WHERE T.LeadDate >= DATEADD(hh,7,DATEDIFF(D, 0, GETDATE()))

没有必要将DATEDIFF(D, 0, GETDATE())部分回溯到某个时间

于 2012-12-11T20:45:37.363 回答
0

为了完整起见,考虑到您的规则,这将使您每天计数:

-- 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)
于 2012-12-11T20:52:42.117 回答