对于sql server 2008管理工作室中的存储过程,我正在尝试构建以下内容:
SELECT MAX(Orderdatum) AS Orderdatum,
klantnummer
FROM Klantenkaart
GROUP BY klantnummer
这给了我MAX
orderdatum
但我希望结合:
orderdatum< DATEADD(年,-2,SYSDATETIME())
所以我想选择max(orderdatum)= 2年前的记录。
我该怎么做?
答案 0 :(得分:2)
尝试此操作,将having
与group by
select MAX(Orderdatum)As Orderdatum, klantnummer from Klantenkaart
group by klantnummer
having orderdatum < DATEADD(YEAR, -2, SYSDATETIME())
答案 1 :(得分:1)
检查一下:
SELECT
MAX(Orderdatum) AS Orderdatum,
klantnummer
FROM Klantenkaart
WHERE orderdatum > DATEADD(year,-2,GETDATE())
GROUP BY klantnummer
答案 2 :(得分:0)
只需将您的条件作为WHERE子句;
SELECT MAX(Orderdatum)As Orderdatum, klantnummer
FROM Klantenkaart
WHERE orderdatum < DATEADD(YEAR, -2, SYSDATETIME())
GROUP BY klantnummer