0

我正在尝试统计昨天每小时发生的所有交易。我不希望今天进行交易。但我似乎无法在这种情况下获得正确的代码。

到目前为止我的代码:

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 加入?

4

3 回答 3

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))
于 2017-09-06T18:26:56.497 回答
1

如果我理解您的问题,您可以执行以下操作:

Where   TransDT >= Convert(Date, DateAdd(Day, -1, GetDate())
And     TransDT < Convert(Date, GetDate())
于 2017-09-06T18:26:45.200 回答
0

TransDT 是一个日期,但您与一个 INT 进行比较。

尝试这样的事情:

TransDT = CAST(SYSDATETIME() AS DATE)
于 2017-09-06T18:27:59.477 回答