0

我希望将我的数据放在具有多行而不是一行的 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

期望的结果: 在此处输入图像描述

4

0 回答 0