根据客户分组商品销售前3名

SELECT *
FROM (
         SELECT vl.DM1               AS 客户代码,
                k.KHMC                AS 客户名称,
                vl.SPDM               AS 商品代码,
                s.SPMC                AS 商品名称,
                CAST(SUM(SL) AS INT)  AS 销售数量,
                DENSE_RANK()OVER(PARTITION BY vl.dm1 ORDER BY SUM(SL) DESC) AS 名次
         FROM VW_LSXHDMX             AS vl
                LEFT JOIN KEHU       AS k ON  k.KHDM = vl.DM1
                LEFT JOIN SHANGPIN   AS s ON  s.SPDM = vl.SPDM
         GROUP BY vl.dm1 , vl.SPDM , k.KHMC , s.SPMC
     ) T1
WHERE t1.[名次] <= 3
ORDER BY  t1.[客户代码] , T1.[名次]

posted @ 2017-12-18 08:46  liessay  阅读(312)  评论(0编辑  收藏  举报