SQL分组多维数据集和数据透视表

时间:2016-11-16 09:28:43

标签: sql oracle pivot grouping cube

我尝试执行以下查询,在此处获取按年,月和网站分组的表格,然后将此网站转移到列:

<android.support.v7.widget.RecyclerView
    android:id="@+id/trip_ticket_recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

获得此结果

SELECT * FROM 
(
SELECT 
  DECODE(GROUPING(TO_CHAR(TM.TIMESTAMP,'YYYY'))
    ,0, TO_CHAR(TM.TIMESTAMP,'YYYY') 
    ,1, 'TOTAL') AS "YEAR",

  DECODE(GROUPING(TO_CHAR(TM.TIMESTAMP,'MM'))
    ,0, TO_CHAR(TM.TIMESTAMP,'MM') 
    ,1, 'TOTAL') AS "MONTH",  

  DECODE(GROUPING(TS.CODIGO5)
    ,0, TS.CODIGO5
    ,1, 'TOTAL') AS BU,

  SUM(TM.KWHGEN) AS GEN

FROM T_MEDIDAS_CO TM
    JOIN T_Sede TS ON TM.id_sede=TS.id_sede
  WHERE TO_CHAR(TM.TIMESTAMP,'YYYY') IN (2015,2014)
  AND TS.CODIGO5 IN ('FINSI', 'FINOC')
    GROUP BY CUBE (TO_CHAR(TM.TIMESTAMP,'YYYY'), TO_CHAR(TM.TIMESTAMP,'MM'), TS.CODIGO5)
 ORDER BY TO_CHAR(TM.TIMESTAMP,'YYYY') DESC, TO_CHAR(TM.TIMESTAMP,'MM') DESC, 3

)
PIVOT 
(
   SUM(GEN)
   FOR BU IN ('FINCI' AS FINCI,'FINSI' AS FINSI, 'FINOC' AS FINOC, 'TOTAL' AS TOTAL)
)

ORDER BY "YEAR" DESC, "MONTH" DESC

但是,我不需要TOTAL | MONTH行,我该如何解决?

非常感谢

0 个答案:

没有答案