我试图找到最早的唯一ID日期实例(唯一ID包含多行具有不同日期的数据),然后使用子查询过滤该结果以查找2005年和2010年之间的数据。我一直在关键字'''附近找到'不正确的语法:
Select *
from (select custnum, YEAR(min(Date_field)) as field1, Field2, field3, field4, field5
from table1
group by custnum, field2, field3, field4, field5)
having YEAR(min(Date_field)) between 2005 and 2010
任何想法? 谢谢你的帮助 -
答案 0 :(得分:0)
如果我理解正确,您可以使用:
WITH CTE AS
(
SELECT custnum,
Field2,
Field3,
Field4,
Field5,
Field1 = MIN(Date_Field) OVER(PARTITION BY custnum)
FROM dbo.table1
)
SELECT *
FROM CTE
WHERE Field1 BETWEEN '20050101' AND '20101231';