如何在 SQL Server 2014 中获取昨天和今天之间的交易?
How to get transactions between yesterday and today in SQL Server 2014?
我正在尝试每小时统计昨天发生的所有交易。我不希望今天进行交易。但我似乎无法在条件下正确获取代码。
到目前为止我的代码:
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 加入?
如果我理解你的问题,你可以进行以下操作:
Where TransDT >= Convert(Date, DateAdd(Day, -1, GetDate())
And TransDT < Convert(Date, GetDate())
你试过吗?
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))
TransDT 是一个日期,但是你比较的是一个 INT。
尝试这样的事情:
TransDT = CAST(SYSDATETIME() AS DATE)
我正在尝试每小时统计昨天发生的所有交易。我不希望今天进行交易。但我似乎无法在条件下正确获取代码。
到目前为止我的代码:
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 加入?
如果我理解你的问题,你可以进行以下操作:
Where TransDT >= Convert(Date, DateAdd(Day, -1, GetDate())
And TransDT < Convert(Date, GetDate())
你试过吗?
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))
TransDT 是一个日期,但是你比较的是一个 INT。
尝试这样的事情:
TransDT = CAST(SYSDATETIME() AS DATE)