在Oracle中使用分组集时,使用desc进行排序

时间:2016-07-27 12:28:40

标签: oracle

我有以下查询

select to_char(WK_END_DT) AS MTD,CASE WHEN EMP_NAME IS NULL
                THEN 
                'TOTAL'
                ELSE 
                EMP_NAME END AS EMPLOYEE, sum(Trans) AS Trans,sum(Units) AS Units,sum(Sales) AS Sales,sum(UPT) AS UPT,sum(ADS) AS ADS,sum(FICCount) AS FICCount,sum(FICDollars) AS FICDollars,sum(FICPercent) AS FICPercent  from 
                (
                SELECT a16.WK_END_DT,
                         a15.EMP_NAME,
                         COUNT (DISTINCT a11.TXN_NUM) Trans,
                         (  (  NVL (SUM (a11.F_CLRC_SLS_QTY), 0)
                             + NVL (SUM (a11.F_PRMTN_SLS_QTY), 0))
                          + NVL (SUM (a11.F_RGLR_SLS_QTY), 0))
                            Units,
                         (  (  NVL (SUM (a11.F_CLRC_SLS_AMT), 0)
                             + NVL (SUM (a11.F_PRMTN_SLS_AMT), 0))
                          + NVL (SUM (a11.F_RGLR_SLS_AMT), 0)+ NVL (SUM (a11.F_FBC_AMT),0))
                            Sales,
                           round((  (  NVL (SUM (a11.F_CLRC_SLS_QTY), 0)
                               + NVL (SUM (a11.F_PRMTN_SLS_QTY), 0))
                            + NVL (SUM (a11.F_RGLR_SLS_QTY), 0))
                         / COUNT (DISTINCT a11.TXN_NUM),2)
                            UPT,
                         round((  (  (  NVL (SUM (a11.F_CLRC_SLS_AMT), 0)
                                + NVL (SUM (a11.F_PRMTN_SLS_AMT), 0))
                             + NVL (SUM (a11.F_RGLR_SLS_AMT), 0))
                          / COUNT (DISTINCT a11.TXN_NUM)),2)
                            ADS,
                         SUM (a11.F_FBC_QTY) FICCount,
                         SUM (a11.F_FBC_AMT) FICDollars,
                         (round((SUM (a11.F_FBC_QTY) / COUNT (DISTINCT a11.TXN_NUM)),3))*100 FICPercent
                    FROM rdw30_adm_dm.WSL_SLS_TRAN_SKU_LH_DM@PRDRDW.wetseal.com a11,
                         rdw30_adm_dm.TIME_DAY_DM@PRDRDW.wetseal.com a12,
                         rdw30_adm_dm.ORG_LOC_DM@PRDRDW.wetseal.com a14,
                         rdw30_adm_dm.WSL_SALES_ASSOC_DM@PRDRDW.wetseal.com a15,
                         rdw30_adm_dm.TIME_WK_DM@PRDRDW.wetseal.com a16
                   WHERE     a11.DAY_IDNT = a12.DAY_IDNT
                         AND (    ( (a12.WK_IDNT) IN
                                      (SELECT r11.WK_IDNT
                                         FROM rdw30_adm_dm.TIME_DAY_DM@PRDRDW.wetseal.com r11
                                       WHERE r11.DAY_DT BETWEEN (SELECT min(DAY_DT)  FROM rdw30_adm_dm.time_day_dm@PRDRDW.wetseal.com
          WHERE wk_idnt in 
          (SELECT  WK_IDNT
           FROM rdw30_adm_dm.time_day_dm@PRDRDW.wetseal.com
          WHERE day_dt = (TRUNC (SYSDATE) - 7)))
                                                             AND (SELECT max(DAY_DT)
           FROM rdw30_adm_dm.time_day_dm@PRDRDW.wetseal.com
          WHERE wk_idnt in 
          (SELECT  WK_IDNT
           FROM rdw30_adm_dm.time_day_dm@PRDRDW.wetseal.com
          WHERE day_dt = (TRUNC (SYSDATE) - 7)))))
                              AND a14.LOC_IDNT IN (149))
                         AND a12.WK_IDNT = a16.WK_IDNT
                         AND a11.LOC_KEY = a14.LOC_KEY
                         AND a11.SALES_ASSOC = a15.SALES_ASSOC
                GROUP BY a12.WK_IDNT,
                         a14.LOC_IDNT,
                         a11.SALES_ASSOC,
                         a16.WK_END_DT,
                         a15.EMP_NAME  order by Sales DESC)
               group by grouping sets((WK_END_DT,EMP_NAME),(WK_END_DT))

我试图按销售列降序值和以下格式订购。

    "WK_END_DT" "EMP_NAME"  "TRANS" "UNITS" "SALES" "UPT"   "ADS"   "FICCOUNT"  "FICDOLLARS"    "FICPERCENT"
23-JUL-16   "Lalugba, Nea H"    199 605 5888.38 3.04    29.19   4   80  2
23-JUL-16   "Perez, Vanessa"    143 386 3748.35 2.7 25.23   7   140 4.9
23-JUL-16   "Gamarra, Nadim K"  97  269 2659.59 2.77    26.59   4   80  4.1
23-JUL-16   "Nunez, Katherine"  69  222 2314.01 3.22    31.8    6   120 8.7
23-JUL-16   "Hunter-Bullock, Natasha N" 82  218 2170.88 2.66    25.99   2   40  2.4
23-JUL-16   "Baez, Nadia L" 66  168 1713.98 2.55    25.06   3   60  4.5
23-JUL-16   "Christ, Danielle"  68  168 1618.19 2.47    23.21   2   40  2.9
23-JUL-16   "Taveras, Jarissa"  25  68  877.12  2.72    34.28   1   20  4
23-JUL-16   "Montanez, Naomi"   35  82  873.79  2.34    24.39   1   20  2.9
23-JUL-16   "Melendez, Justin"  20  60  647.82  3   32.39   0   0   0
23-JUL-16   "Leja, Agnieszka L" 7   29  332.93  4.14    44.7    1   20  14.3
23-JUL-16   Total                       22845.04

但是每当我按照上面的场景尝试订购它,即按销售额命令时,总金额就会出现在我不想要的结果集的顶部。有没有办法以我在这个问题中显示的格式订购它。哪个是总金额应该保留在底部,而销售列中剩余的金额按销售列的降序排序。

2 个答案:

答案 0 :(得分:1)

order by case when emp_name = 'Total' then 1 else 0 end, sales

即使没有为SELECT创建标志0或1,它也会按顺序计算;首先通过这个标志排序将保证总数在最后。其他一切都是按销售顺序排列的。

注意:我不相信您运行了您发布的查询并获得了您发布的输出。在查询中,您有' TOTAL'在输出中你有' Total'。在ORDER BY子句中,完全匹配大小写。

此外,您可以在顶部将CASE表达式替换为nvl(emp_name, 'Total')

答案 1 :(得分:1)

  1. group by grouping sets((WK_END_DT,EMP_NAME),(WK_END_DT))

  2. 之后移动您的订单
  3. 您的订单可能如下order by decode(EMP_NAME,null,1,0), sales