Цикл по годам и месяцам в запросах SELECT и UNPIVOT - PullRequest
0 голосов
/ 22 января 2020

Я сделал следующее чудовище запроса, который работает правильно (Мой фактический запрос повторяет то же самое для 2017 и 2018, но количество символов в запросе слишком велико для вопроса переполнения стека).

Можно ли как-нибудь очистить и уменьшить его, используя некие l oop в комбинациях месяца и года в частях SELECT и UNPIVOT?

    SELECT 
        SUBSTRING(ENTITY_MONTH_YEAR, 1, CHARINDEX(' ', ENTITY_MONTH_YEAR)) AS [ENTITY], 
        [L1 SEGMENT],
        [L2 SEGMENT],
        [L1 PRODUCT],
        [L2 PRODUCT],
        MONTH(SUBSTRING(ENTITY_MONTH_YEAR, CHARINDEX(' ', ENTITY_MONTH_YEAR), 4) + ' 01 2000') AS [MONTH],
        CAST(RIGHT(ENTITY_MONTH_YEAR, 4) AS INT) AS [YEAR],
        [REVENUE]
    FROM (
        SELECT
            [EBU Gross].l1_segment AS [L1 SEGMENT],
            [EBU Gross].l2_segment AS [L2 SEGMENT],
            [EBU Gross].l1_product AS [L1 PRODUCT],
            [EBU Gross].l2_product AS [L2 PRODUCT],

            -- TODO: Figure out how to loop through year/month

            CAST(SUM(
                ISNULL([EBU Gross].[2019-01-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-01-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-01-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU Jan 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-01-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-01-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS Jan 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-01-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC Jan 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-01-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-01-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-01-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-01-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-01-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-01-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV Jan 2019',

            CAST(SUM(
                ISNULL([EBU Gross].[2019-02-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-02-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-02-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU Feb 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-02-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-02-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS Feb 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-02-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC Feb 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-02-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-02-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-02-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-02-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-02-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-02-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV Feb 2019',

            CAST(SUM(
                ISNULL([EBU Gross].[2019-03-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-03-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-03-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU Mar 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-03-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-03-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS Mar 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-03-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC Mar 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-03-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-03-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-03-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-03-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-03-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-03-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV Mar 2019',

            CAST(SUM(
                ISNULL([EBU Gross].[2019-04-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-04-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-04-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU Apr 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-04-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-04-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS Apr 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-04-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC Apr 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-04-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-04-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-04-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-04-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-04-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-04-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV Apr 2019',

            CAST(SUM(
                ISNULL([EBU Gross].[2019-05-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-05-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-05-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU May 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-05-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-05-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS May 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-05-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC May 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-05-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-05-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-05-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-05-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-05-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-05-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV May 2019',

            CAST(SUM(
                ISNULL([EBU Gross].[2019-06-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-06-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-06-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU Jun 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-06-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-06-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS Jun 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-06-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC Jun 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-06-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-06-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-06-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-06-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-06-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-06-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV Jun 2019',

            CAST(SUM(
                ISNULL([EBU Gross].[2019-07-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-07-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-07-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU Jul 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-07-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-07-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS Jul 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-07-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC Jul 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-07-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-07-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-07-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-07-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-07-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-07-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV Jul 2019',

            CAST(SUM(
                ISNULL([EBU Gross].[2019-08-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-08-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-08-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU Aug 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-08-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-08-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS Aug 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-08-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC Aug 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-08-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-08-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-08-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-08-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-08-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-08-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV Aug 2019',

            CAST(SUM(
                ISNULL([EBU Gross].[2019-09-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-09-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-09-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU Sep 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-09-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-09-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS Sep 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-09-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC Sep 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-09-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-09-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-09-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-09-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-09-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-09-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV Sep 2019',

            CAST(SUM(
                ISNULL([EBU Gross].[2019-10-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-10-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-10-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU Oct 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-10-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-10-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS Oct 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-10-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC Oct 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-10-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-10-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-10-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-10-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-10-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-10-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV Oct 2019',

            CAST(SUM(
                ISNULL([EBU Gross].[2019-11-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-11-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-11-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU Nov 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-11-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-11-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS Nov 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-11-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC Nov 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-11-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-11-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-11-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-11-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-11-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-11-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV Nov 2019',

            CAST(SUM(
                ISNULL([EBU Gross].[2019-12-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-12-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-12-01 00:00:00], 0)
            ) AS FLOAT) AS 'EBU Dec 2019',
            CAST(SUM(
                ISNULL([Sell Through].[2019-12-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-12-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCS Dec 2019',
            CAST(SUM(
                ISNULL([STCSC].[2019-12-01 00:00:00], 0)
            ) AS FLOAT) AS 'STCSC Dec 2019',
            CAST(SUM(
                ISNULL([EBU Gross].[2019-12-01 00:00:00], 0) + 
                ISNULL([Provider Share].[2019-12-01 00:00:00], 0) - 
                ISNULL([Devices].[2019-12-01 00:00:00], 0) +
                ISNULL([Sell Through].[2019-12-01 00:00:00], 0) + 
                ISNULL([Sell Direct].[2019-12-01 00:00:00], 0) + 
                ISNULL([STCSC].[2019-12-01 00:00:00], 0)
            ) AS FLOAT) AS 'REV Dec 2019'

        FROM (
            SELECT 
                * 
            FROM 
                [dbo].[GL_Revenue_Master] 
            WHERE
                [entity] = 'EBU' AND 
                [account] = 'EBU Gross'
        ) [EBU Gross]

        LEFT JOIN (
            SELECT 
                * 
            FROM 
                [dbo].[GL_Revenue_Master]
            WHERE   
                [entity] = 'EBU' AND 
                [account] = 'Provider Share'
        ) [Provider Share]
        ON
            [EBU Gross].l1_segment = [Provider Share].l1_segment AND 
            [EBU Gross].l2_segment = [Provider Share].l2_segment AND 
            [EBU Gross].l1_product = [Provider Share].l1_product AND 
            [EBU Gross].l2_product = [Provider Share].l2_product

        LEFT JOIN (
            SELECT 
                * 
            FROM 
                [dbo].[GL_Revenue_Master]
            WHERE
                [entity] = 'EBU' AND 
                [account] = 'Devices'
        ) [Devices]
        ON
            [EBU Gross].l1_segment = [Devices].l1_segment AND 
            [EBU Gross].l2_segment = [Devices].l2_segment AND 
            [EBU Gross].l1_product = [Devices].l1_product AND 
            [EBU Gross].l2_product = [Devices].l2_product

        LEFT JOIN (
            SELECT 
                *
            FROM 
                [dbo].[GL_Revenue_Master]
            WHERE 
                [entity] = 'STCS' AND 
                [account] = 'Sell Through'
        ) [Sell Through]
        ON
            [EBU Gross].l1_segment = [Sell Through].l1_segment AND 
            [EBU Gross].l2_segment = [Sell Through].l2_segment AND 
            [EBU Gross].l1_product = [Sell Through].l1_product AND 
            [EBU Gross].l2_product = [Sell Through].l2_product

        LEFT JOIN (
            SELECT 
                * 
            FROM 
                [dbo].[GL_Revenue_Master]
            WHERE 
                [entity] = 'STCS' AND 
                [account] = 'Sell Direct'    
        ) [Sell Direct]
        ON
            [EBU Gross].l1_segment = [Sell Direct].l1_segment AND 
            [EBU Gross].l2_segment = [Sell Direct].l2_segment AND 
            [EBU Gross].l1_product = [Sell Direct].l1_product AND 
            [EBU Gross].l2_product = [Sell Direct].l2_product

        LEFT JOIN (
            SELECT 
                * 
            FROM 
                [dbo].[GL_Revenue_Master]
            WHERE 
                [entity] = 'STCSC' AND 
                [account] = 'Sell Direct'
        ) [STCSC]
        ON
            [EBU Gross].l1_segment = [STCSC].l1_segment AND 
            [EBU Gross].l2_segment = [STCSC].l2_segment AND 
            [EBU Gross].l1_product = [STCSC].l1_product AND 
            [EBU Gross].l2_product = [STCSC].l2_product

        GROUP BY
            [EBU Gross].l1_segment,
            [EBU Gross].l2_segment,
            [EBU Gross].l1_product,
            [EBU Gross].l2_product

    ) _

    UNPIVOT (
        REVENUE
        FOR 
            ENTITY_MONTH_YEAR
        -- TODO: Figure out how to loop over year/month
        IN (
            [EBU Jan 2019],
            [STCS Jan 2019],
            [STCSC Jan 2019],
            [REV Jan 2019],

            [EBU Feb 2019],
            [STCS Feb 2019],
            [STCSC Feb 2019],
            [REV Feb 2019],

            [EBU Mar 2019],
            [STCS Mar 2019],
            [STCSC Mar 2019],
            [REV Mar 2019],

            [EBU Apr 2019],
            [STCS Apr 2019],
            [STCSC Apr 2019],
            [REV Apr 2019],

            [EBU May 2019],
            [STCS May 2019],
            [STCSC May 2019],
            [REV May 2019],

            [EBU Jun 2019],
            [STCS Jun 2019],
            [STCSC Jun 2019],
            [REV Jun 2019],

            [EBU Jul 2019],
            [STCS Jul 2019],
            [STCSC Jul 2019],
            [REV Jul 2019],

            [EBU Aug 2019],
            [STCS Aug 2019],
            [STCSC Aug 2019],
            [REV Aug 2019],

            [EBU Sep 2019],
            [STCS Sep 2019],
            [STCSC Sep 2019],
            [REV Sep 2019],

            [EBU Oct 2019],
            [STCS Oct 2019],
            [STCSC Oct 2019],
            [REV Oct 2019],

            [EBU Nov 2019],
            [STCS Nov 2019],
            [STCSC Nov 2019],
            [REV Nov 2019],

            [EBU Dec 2019],
            [STCS Dec 2019],
            [STCSC Dec 2019],
            [REV Dec 2019]

        )
    ) _
...