在where子句中使用MIN

时间:2016-06-17 14:06:29

标签: sql tsql min

尝试更改以下代码以允许我在where子句

中使用min
SELECT od.personID
,'Uno' AS [CustomerType]
FROM dbo.orderDetails od
WHERE od.orderDate BETWEEN CONVERT(date, '2016-02-01') AND @StartDate
AND MIN(odd.orderDate) BETWEEN CONVERT(date, '2016-02-01') AND @StartDate)
GROUP BY personID

1 个答案:

答案 0 :(得分:6)

MIN移至HAVING部分

SELECT od.personID
,'Uno' AS [CustomerType]
FROM dbo.orderDetails od
WHERE od.orderDate BETWEEN CONVERT(date, '2016-02-01') AND @StartDate     
GROUP BY personID
HAVING MIN(odd.orderDate) BETWEEN CONVERT(date, '2016-02-01') AND @StartDate