使用窗口函数获取总计数

时间:2016-09-16 19:54:37

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

嘿所有这些是我到目前为止的查询:

WITH LIMIT AS
        (SELECT 
             U.userID
            ,U.username
            ,U.fname    
            ,U.mname    
            ,U.lname    
            ,U.email    
            ,U.active 
            ,S.sName
            ,S.sID
            ,T.[value]
            ,T.trackingNumberID
            ,SU.primaryLocation
            ,row_number() OVER (ORDER BY U.userid) AS RN 
            ,COUNT(*) OVER (ORDER BY U.userid) AS CNT 
            ,UR.roleID
        FROM 
            [---].[dbo].[tblUsers]                          AS U
        LEFT OUTER JOIN [---].[dbo].[tblTrackingNumbers]    AS T
            ON T.userID = U.userID
        LEFT OUTER JOIN [---].[dbo].[tblSU]                 AS SU
            ON U.userID = SU.userID
        LEFT OUTER JOIN [---].[dbo].[tblS]                  AS S
            ON SU.sID = S.sID 
        LEFT OUTER JOIN [---].[dbo].[tblUserRoles]          AS UR 
            ON UR.userID = U.userID
        LEFT OUTER JOIN [---].[dbo].[tblRoles]              AS R 
            ON UR.roleID = R.roleID
        WHERE 
            U.active = 1 
        AND 
            SU.primaryLocation = 1
        AND 
            SU.active = 1 
        AND 
                U.orgID = 1
            AND 
                S.ID = 35 
            AND U.userID IN (SELECT userID 
                              FROM [---].[dbo].[tblSU] AS SU 
                              INNER JOIN [].[dbo].[tblS] AS S 
                                 ON S.sID = SU.sID 
                              WHERE 
                                 SU.active = 1 
                              AND 
                                 S.sID = 35)
) SELECT * FROM LIMIT WHERE RN Between 0 AND 10000

正如您在上面的查询中所看到的,我正在尝试 COUNT(*)OVER(ORDER BY U.userid)AS CNT ,这给了我与 RN 相同的计数

我需要的是记录总数,这将带回(842行)

2 个答案:

答案 0 :(得分:2)

COUNT(*) OVER (ORDER BY U.userid) AS CNT计算"运行计数" - 计数直到""行。如果要计算完整结果中的所有行,请使用不带order by

的窗口函数
COUNT(*) OVER () AS CNT

答案 1 :(得分:1)

这可能听起来像杜鹃,但是如果您选择计数到变量然后选择您的记录并添加变量,我会发现大表会获得更好的性能。当表格变得太大时,count(*)over()会导致性能下降。

DECLARE @RecordCount INT
SELECT  @RecordCount = COUNT(*)
FROM    [---].[dbo].[tblUsers] AS U
        LEFT OUTER JOIN [---].[dbo].[tblTrackingNumbers] AS T ON T.userID = U.userID
        LEFT OUTER JOIN [---].[dbo].[tblSU] AS SU ON U.userID = SU.userID
        LEFT OUTER JOIN [---].[dbo].[tblS] AS S ON SU.sID = S.sID
        LEFT OUTER JOIN [---].[dbo].[tblUserRoles] AS UR ON UR.userID = U.userID
        LEFT OUTER JOIN [---].[dbo].[tblRoles] AS R ON UR.roleID = R.roleID
WHERE   U.active = 1
        AND SU.primaryLocation = 1
        AND SU.active = 1
        AND U.orgID = 1
        AND S.ID = 35
        AND U.userID IN (SELECT userID
                         FROM   [---].[dbo].[tblSU] AS SU
                                INNER JOIN [].[dbo].[tblS] AS S ON S.sID = SU.sID
                         WHERE  SU.active = 1
                                AND S.sID = 35)

SELECT  U.userID,
        U.username,
        U.fname,
        U.mname,
        U.lname,
        U.email,
        U.active,
        S.sName,
        S.sID,
        T.[value],
        T.trackingNumberID,
        SU.primaryLocation,
        @RecordCount AS CNT,
        UR.roleID
FROM    [---].[dbo].[tblUsers] AS U
        LEFT OUTER JOIN [---].[dbo].[tblTrackingNumbers] AS T ON T.userID = U.userID
        LEFT OUTER JOIN [---].[dbo].[tblSU] AS SU ON U.userID = SU.userID
        LEFT OUTER JOIN [---].[dbo].[tblS] AS S ON SU.sID = S.sID
        LEFT OUTER JOIN [---].[dbo].[tblUserRoles] AS UR ON UR.userID = U.userID
        LEFT OUTER JOIN [---].[dbo].[tblRoles] AS R ON UR.roleID = R.roleID
WHERE   U.active = 1
        AND SU.primaryLocation = 1
        AND SU.active = 1
        AND U.orgID = 1
        AND S.ID = 35
        AND U.userID IN (SELECT userID
                         FROM   [---].[dbo].[tblSU] AS SU
                                INNER JOIN [].[dbo].[tblS] AS S ON S.sID = SU.sID
                         WHERE  SU.active = 1
                                AND S.sID = 35)
ORDER BY U.userID
OFFSET 0 ROWS FETCH NEXT 10000 ROWS ONLY