仅包含某些变量的COUNT之和

时间:2018-12-13 14:17:28

标签: sql oracle count sum partition

我在下面的查询中返回结果,但是我需要将某些变量从Filled_orders列中排除。

整个查询:

SELECT
    order_date,
    p_category,
    issue_group,
    srt_level,
    order_count,
    filled_orders,
    total_orders,
    round(100 *(order_count / total_orders), 1) AS monthly_metric
FROM
    (
        SELECT
            order_date,
            p_category,
            issue_group,
            srt_level,
            order_count,
            CASE
                WHEN srt_level = '80'  THEN order_count
                WHEN srt_level = '100' THEN SUM(order_count) OVER(
                    PARTITION BY order_date, issue_group, p_category
                )
            END AS filled_orders,
            total_orders
        FROM
            (
                SELECT
                    order_date,
                    p_category,
                    issue_group,
                    srt_level,
                    order_count,
                    SUM(order_count) OVER(
                        PARTITION BY order_date, issue_group, p_category
                    ) AS total_orders
                FROM
                    (
                        SELECT
                            order_date,
                            p_category,
                            CASE
                                WHEN ( issue_grp = 1 ) THEN '1'
                                ELSE '2/3 '
                            END AS issue_group,
                            srt   AS srt_level,
                            COUNT(*) AS order_count
                        FROM
                            database.tcon_mv
                        WHERE
                            order_date IN (
                                '201803'
                            )
                        GROUP BY
                            p_category,
                            CASE
                                WHEN ( issue_grp = 1 ) THEN '1'
                                ELSE '2/3 '
                            END,
                            srt,
                            order_date
                    )
            )
    )
ORDER BY
    order_date,
    p_category,
    issue_group

给我“问题”的部分:

... SELECT
                order_date,
                p_category,
                issue_group,
                srt_level,
                order_count,
                CASE
                    WHEN srt_level = '80'  THEN order_count
                    WHEN srt_level = '100' THEN SUM(order_count) OVER(
                        PARTITION BY order_date, issue_group, p_category
                    )
                END AS filled_orders,
                total_orders
            FROM ....

srt_level是一个varchar列,其结果只有3个选项(80、100和Late)。当srt_level ='100'时,我需要计数仅包括80行和100行的总和。

完整查询返回的视图,预期结果用红色表示。

enter image description here

1 个答案:

答案 0 :(得分:0)

我假设您只想对order_count不是srt_level的{​​{1}}值求和。然后在您的Late中说:

sum