SQL函数之:排名函数

排名函数,取前五名:

SELECT
    bb.rowNumber,
    bb.unitName,
    bb.allSum 
FROM
    (
    SELECT
        ROW_NUMBER ( ) OVER ( ORDER BY allSum DESC ) AS rowNumber,* 
    FROM
        (
        SELECT
            unitName,
            SUM ( a.Debit ) - SUM ( a.Credit ) AS allSum 
        FROM
            ( SELECT * FROM CN_AcctBook WHERE AcctDate BETWEEN  '2021-10-01 00:00:00.000' AND '2021-10-30 00:00:00.000' ) a
            LEFT OUTER JOIN CN_Unit b ON a.UnitID = b.ID
            LEFT OUTER JOIN Customer c ON b.LoadCusID = c.cCusCode
            JOIN CN_Custom245 f ON a.CustomCol1= f.ID 
        WHERE
            f.field1 = '主营回款' 
        GROUP BY
            unitName 
        ) aa 
    ) bb 
WHERE
    bb.rowNumber BETWEEN 1 
    AND 5;

结果:

 

posted @ 2021-12-01 17:02  优秀的进度条  阅读(307)  评论(0编辑  收藏  举报