透视或案例陈述?

时间:2013-09-23 17:10:08

标签: db2 pivot recursive-query

我希望在location_id上转移我的数据,而不是一行。我有多个位置,我需要一种更简单的方法来转移我的数据并计算几个总计,而不必每次都对location_name进行硬编码。不知道如何接近这一点,并希望推动正确的方向。 DB2数据库。

SELECT RowNumber () over (PARTITION BY DIM_LOCATION_ID ORDER BY LOCATION_NAME),

        DIM_LOCATION_ID,
        LOCATION_NAME,

        (
        SELECT 
        SUM(SALES_AMOUNT) AS SALES_SUMMARY
        FROM FACT_TABLE
        WHERE LOCATION_NAME = 'DENVER'

        ) AS SALES_SUMMARYDV,

        (
        SELECT 
        SUM(SALES_AMOUNT) AS SALES_SUMMARY
        FROM FACT_TABLE
        WHERE LOCATION_NAME = 'CHICAGO'

        ) AS SALES_SUMMARYCH,

            (
        SELECT
        SUM(ORDERS) AS SUMMARY_ORDERS
        FROM FACT_TABLE
        WHERE LOCATION_NAME = 'DENVER'

        ) AS ORDER_SUMMARYDV,


        (
        SELECT
        SUM(ORDERS) AS SUMMARY_ORDERS
        FROM FACT_TABLE
        WHERE LOCATION_NAME = 'CHICAGO'

        ) AS ORDER_SUMMARYCH



    FROM FACT_TABLE 
    GROUP BY DIM_LOCATION_ID, LOCATION_NAME  
    ORDER BY LOCATION_NAME

期望的结果: enter image description here

0 个答案:

没有答案