SQL Server 2014中的窗口函数

时间:2014-09-23 16:58:54

标签: sql window-functions sql-server-2014

有谁知道你是否可以在SQL Server 2014的set语句中使用窗口函数?

我正在尝试将现有表中的列设置为来自另一个表的结果计数。 我可以使用临时表来做到这一点,只是认为它具有窗口函数会更干净。

Update #Totals
  Set 
    TotalContacts = Count(C.PatientID) Over (Partition By C.HospCode)
  From
  #Totals as T
  Inner Join
  #Contacts as C
  On
  T.HospCode = C.HospCode

1 个答案:

答案 0 :(得分:1)

没有

SQL Server 2014不允许在set中直接使用窗口函数。这不是标准的SQL。

自2005年以来,你已经能够做到这一点。

WITH CTE 
     AS (SELECT TotalContacts, 
                Count(C.PatientID) 
                  OVER (Partition BY C.HospCode) AS Cnt
         FROM   Totals AS T 
                INNER JOIN Contacts AS C 
                        ON T.HospCode = C.HospCode) 
UPDATE CTE 
SET    TotalContacts = Cnt