0

对于 sql server 2008 management studio 中的存储过程,我正在尝试构建以下内容:

SELECT MAX(Orderdatum) AS Orderdatum, 
       klantnummer 
FROM   Klantenkaart 
GROUP BY klantnummer

这给了我MAXorderdatum 但我想结合: orderdatum < DATEADD(YEAR, -2, SYSDATETIME())

所以我想要选择 max(orderdatum) = 2 年前的记录。

我怎样才能做到这一点?

4

3 回答 3

2

试试这个,having使用group by

select MAX(Orderdatum)As Orderdatum, klantnummer from Klantenkaart 
 group by klantnummer
 having orderdatum < DATEADD(YEAR, -2, SYSDATETIME())
于 2013-09-23T11:50:25.203 回答
1

检查这个:

SELECT 
 MAX(Orderdatum) AS Orderdatum, 
 klantnummer 
FROM Klantenkaart
 WHERE orderdatum > DATEADD(year,-2,GETDATE()) 
GROUP BY klantnummer
于 2013-09-23T11:58:07.580 回答
0

只需将您的条件作为 WHERE 子句;

SELECT MAX(Orderdatum)As Orderdatum, klantnummer 
FROM   Klantenkaart 
WHERE  orderdatum < DATEADD(YEAR, -2, SYSDATETIME())
GROUP BY klantnummer
于 2013-09-23T11:51:13.780 回答