我试图提取每小时昨天发生的所有交易的数量。我不想今天做交易。但我似乎无法在这种情况下获得正确的代码。
到目前为止我的代码:
SELECT CAST(TransDT as DATE) as 'TransDate'
,CAST(DATEPART(Hour, TransDT) as varchar) + ':00' as 'Hour'
,LaneID
,Direction
,COUNT(*) as 'Ct'
FROM Traffic_analysis
WHERE TransDT >= DATEADD (DAY, -1 , SYSDATETIME())
AND TransDT < CAST(DATEPART(DAY, SYSDATETIME()) as int) --This is the faulty line.
GROUP BY CAST(TransDT as DATE), DATEPART(Hour, TransDT), LaneID, Direction
ORDER BY CAST(TransDT as DATE) ASC
如果我评论整个错误的行,我会得到昨天和今天的结果。我昨天只需要结果。
我要做的是显示一个带有图表的报告,该图表显示每个时段,每个车道,每个方向的流量。将所有NB流量放在一列中并将所有SB流量放在另一列中会更好。也许是UNION加入?
答案 0 :(得分:1)
如果我理解您的问题,您可以执行以下操作:
Where TransDT >= Convert(Date, DateAdd(Day, -1, GetDate())
And TransDT < Convert(Date, GetDate())
答案 1 :(得分:1)
WHERE cast(TransDT as date) > DATEADD (DAY, -2 , cast(SYSDATETIME() as date))
AND cast(TransDT as date) <= DATEADD (DAY, -1 , cast(SYSDATETIME() as date))
实际上,我认为这是理所当然的。
WHERE cast(TransDT as date) = DATEADD (DAY, -1 , cast(SYSDATETIME() as date))
答案 2 :(得分:0)
TransDT是一个日期,但是您与INT进行比较。
尝试类似的东西:
TransDT = CAST(SYSDATETIME() AS DATE)