SQL 无法识别服务器 10.50 OVER PARTITION

SQL Server 10.50 OVER PARTITION not recognized

我正在尝试在 SQL Server 2008 R2 (v10.50.1600.1) 中使用 OVER PARTITION,但是写入和执行它表明在 ORDER 附近出现错误,我尝试 运行 在 SQL Server 2014 (v12.0.4100.1) 中未经修改的相同查询,但它没有问题,并且在 SQL 服务器文档中我没有找到任何东西,我知道那里还有其他方法可以模拟这个过程,但我想知道是否有更敏捷的等效方法。谢谢

SELECT 
    e.ID, e.[date], 
    COUNT(*) OVER (PARTITION BY e.ID ORDER BY e.[date] DESC) AS ind
FROM 
    events e 
JOIN 
    Manager h ON h.ID = e.ID AND h.Active = 1 AND h.[Status] <> 30037 
WHERE 
    e.Iary IN (1, 2, 3) 
GROUP BY 
    e.ID, e.[date]

我收到这个错误:

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'ORDER'

删除ORDER BY e.[date] DESC

SELECT e.ID, e.[date], COUNT(*) OVER(PARTITION BY e.ID) AS ind
FROM events e 
JOIN Manager h ON h.ID = e.ID AND h.Active = 1 AND h.[Status] <> 30037 
WHERE e.Iary IN (1,2,3) 
GROUP BY e.ID, e.[date]

您的查询应如下所示。您只想获得每个 e.ID

count()
SELECT e.ID, e.[date], COUNT(*) AS ind
        FROM events e 
        JOIN Manager h ON h.ID = e.ID AND h.Active = 1 AND h.[Status] <> 30037 
        WHERE e.Iary IN (1,2,3) 
        GROUP BY e.ID, e.[date]
        ORDER BY e.[date]

COUNT()是为了保留那个组的一个程序账号,但是已经解决了,在10.50中使用ORDER BY和COUNT()是不兼容的,如果它工作并且满足objective 在 12.0 中,将 COUNT (*) 更改为 ROW_NUMBER ()

SELECT e.ID, e.[date], ROW_NUMBER() OVER (PARTITION BY e.ID ORDER BY e.[date] DESC) AS ind FROM events e JOIN Manager h ON h.ID = e.ID AND h.Active = 1 AND h.[Status] <> 30037 WHERE e.Iary IN (1, 2, 3) GROUP BY e.ID, e.[date]