0

我要做的就是Scan从每个''中提取最新的''(因此是'LastScan')location,然后找出它们中的任何一个是否超过2小时或更多' old'(这会使它们过期)。

Select cast(max(entered_date) as int) as 'LastScan', location
FROM housing_activity
WHERE 'LastScan' <= Datediff(HH,-2,GETDATE())
Group by location

这是我的错误:

Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value 'LastScan' to data type int.
4

1 回答 1

0

尝试这个

; WITH cte AS (
SELECT
  location
  , LastScan = entered_date
  , RN = ROW_NUMBER() OVER (PARTITION BY location ORDER BY entered_date DESC)
FROM housing_activity 
)
SELECT 
  *
FROM cte
WHERE 
   RN = 1
   AND LastScan <= DateAdd(hour,-2,GETDATE())
于 2020-08-27T03:00:11.003 回答