“分组依据”未按预期分组

问题描述 投票:0回答:1

我创建了以下查询,旨在按日期和 30 分钟时间段分隔数据库信息。

它可能看起来很复杂,但它只是一个日期列,一个时间段列,一堆根据需要对信息进行分类的列和四个计数。

SELECT
    CAST("oo"."created" AS DATE) AS "date",
    
    TO_CHAR(
        DATEADD(
            MINUTE,
            CAST(
                FLOOR(
                    DATEDIFF(MINUTE, '1000-01-01 00:00:00', "oo"."created") / CAST(30 AS DECIMAL)
                ) * 30 AS INTEGER
            ),
            '1000-01-01 00:00:00'
        ),
        'HH24:MI'
    ) AS "time_bucket",
    
    CASE
        WHEN "oo"."decision_date" IS NULL THEN NULL
        WHEN "ooa"."created" IS NULL THEN NULL
        WHEN NOT("dodb"."precise_type" = 'HODN' OR "dodb"."precise_type" = 'HOD') THEN NULL
        WHEN DATEDIFF(SECONDS, "oo"."decision_date", "ooa"."created") <= 3 THEN 
            CASE
                WHEN DATEDIFF(HOUR, "oo"."created", "ooa"."created") <= 3 THEN 'IN'
                ELSE 'OUT'
            END
        ELSE NULL
    END AS "sla",
    
    CASE
        WHEN "oo"."product_name" = 'ASSIST' THEN 'API'
        WHEN "oo"."product_name" = 'BUSINESS' THEN 'B2B'
        WHEN "oard"."logic_name" = 'TRAVEL AND HOTELS Hold' THEN 'T&H'
        WHEN "pccar"."amount" >= 1000 THEN 'H$D'
        WHEN "oard"."logic_name" ILIKE '%UYO%' THEN 'UYO'
        WHEN "oo"."product_name" = 'MALL' THEN 'MALL'
        WHEN "pccar"."brand_code" = 'BUCKS' THEN 'BUX'
        ELSE 'ALL'
    END AS "queue",
    
    CASE
        WHEN "oo"."decision_date" IS NULL AND "ooa"."created" IS NULL THEN 'WAITING REPLY'
        ELSE 'DECISIONED'
    END AS "status",
    
    CASE
        WHEN ("oo"."decision_date" IS NOT NULL AND "ooa"."created" IS NULL) THEN 'SYSTEM'
        WHEN "ooa"."username" IS NOT NULL THEN 'AGENT'
        WHEN ("dodb"."precise_type" = 'HOD' OR "dodb"."precise_type" = 'HODN') THEN 'AGENT'
        ELSE 'SYSTEM'
    END AS "decider",
    
    CASE
        WHEN "ooa"."created" IS NULL THEN NULL
        WHEN ("dodb"."precise_type" = 'HOD' OR "dodb"."precise_type" = 'HODN') THEN 'EXPECTED'
        ELSE 'UNEXPECTED'
    END AS "planned",
    
    CASE
        WHEN "oo"."decision_date" IS NULL THEN 'WR'
        WHEN "ooa"."created" IS NULL THEN '1D: SYSTEM'
        WHEN DATEDIFF(SECONDS, "oo"."decision_date", "ooa"."created") <= 3 THEN '1D: AGENT'
        ELSE 'ND: AGENT'
    END AS "touch",
    
    CASE
        WHEN "ooa"."username" IS NULL THEN NULL
        WHEN "ooa"."username" LIKE '%@A.com{MANAGEMENT}' THEN 'A'
        WHEN "ooa"."username" LIKE '%@b.com{MANAGEMENT}' THEN 'B'
        WHEN "ooa"."username" LIKE '%@u.com.do{MANAGEMENT}' THEN 'U'
        WHEN "ooa"."username" LIKE '%@c.com{MANAGEMENT}' THEN 'C'
        ELSE '@OTHER'
    END AS "domain",

    COUNT("oo"."id") AS "oo.count",

    COUNT(DISTINCT "oo"."id") AS "oo.dcount",

    COUNT("ooa"."id") AS "ooa.count",

    COUNT(DISTINCT "ooa"."id") AS "ooa.dcount"

FROM
    "TAB1"."1" "OO"
    LEFT OUTER JOIN "TAB1"."A" "PCCAR" ON "OO"."O" = "PCCAR"."auth_token"
    LEFT OUTER JOIN "TAB1"."B" "OARD" ON "OO"."id" = "OARD"."riskprovider_order_id"
    LEFT OUTER JOIN "TAB1"."C" "OOA" ON "OO"."id" = "OOA"."order_id"
    LEFT OUTER JOIN "TAB1"."D" "DROD" ON "OO"."id" = "DROD"."order_id"
    LEFT OUTER JOIN "TAB1"."E" "DODB" ON "DROD"."F" = "DODB"."id"

WHERE
    ("OO"."created" >= CAST(DATEADD(DAY, -3, GETDATE()) AS DATE))
    AND ("OO"."created" < CAST(DATEADD(DAY, 0, GETDATE()) AS DATE))

GROUP BY
    CAST("oo"."created" AS DATE) AS "date ",
    
    TO_CHAR(
        DATEADD(
            MINUTE,
            CAST(
                FLOOR(
                    DATEDIFF(MINUTE, '1000-01-01 00:00:00', "oo"."created") / CAST(30 AS DECIMAL)
                ) * 30 AS INTEGER
            ),
            '1000-01-01 00:00:00'
        ),
        'HH24:MI'
    ) AS "time_bucket ",
    
    CASE
        WHEN "oo"."decision_date" IS NULL THEN NULL
        WHEN "ooa"."created" IS NULL THEN NULL
        WHEN NOT("dodb"."precise_type" = 'HODN' OR "dodb"."precise_type" = 'HOD') THEN NULL
        WHEN DATEDIFF(SECONDS, "oo"."decision_date", "ooa"."created") <= 3 THEN 
            CASE
                WHEN DATEDIFF(HOUR, "oo"."created", "ooa"."created") <= 3 THEN 'IN'
                ELSE 'OUT'
            END
        ELSE NULL
    END AS "sla ",
    
    CASE
        WHEN "oo"."product_name" = 'ASSIST' THEN 'API'
        WHEN "oo"."product_name" = 'BUSINESS' THEN 'B2B'
        WHEN "oard"."logic_name" = 'TRAVEL AND HOTELS Hold' THEN 'T&H'
        WHEN "pccar"."amount" >= 1000 THEN 'H$D'
        WHEN "oard"."logic_name" ILIKE '%UYO%' THEN 'UYO'
        WHEN "oo"."product_name" = 'MALL' THEN 'MALL'
        WHEN "pccar"."brand_code" = 'BUCKS' THEN 'BUX'
        ELSE 'ALL'
    END AS "queue",
    
    CASE
        WHEN "oo"."decision_date" IS NULL AND "ooa"."created" IS NULL THEN 'WAITING REPLY'
        ELSE 'DECISIONED'
    END AS "status",
    
    CASE
        WHEN ("oo"."decision_date" IS NOT NULL AND "ooa"."created" IS NULL) THEN 'SYSTEM'
        WHEN "ooa"."username" IS NOT NULL THEN 'AGENT'
        WHEN ("dodb"."precise_type" = 'HOD' OR "dodb"."precise_type" = 'HODN') THEN 'AGENT'
        ELSE 'SYSTEM'
    END AS "decider",
    
    CASE
        WHEN "ooa"."created" IS NULL THEN NULL
        WHEN ("dodb"."precise_type" = 'HOD' OR "dodb"."precise_type" = 'HODN') THEN 'EXPECTED'
        ELSE 'UNEXPECTED'
    END AS "planned",
    
    CASE
        WHEN "oo"."decision_date" IS NULL THEN 'WR'
        WHEN "ooa"."created" IS NULL THEN '1D: SYSTEM'
        WHEN DATEDIFF(SECONDS, "oo"."decision_date", "ooa"."created") <= 3 THEN '1D: AGENT'
        ELSE 'ND: AGENT'
    END AS "touch",
    
    CASE
        WHEN "ooa"."username" IS NULL THEN NULL
        WHEN "ooa"."username" LIKE '%@A.com{MANAGEMENT}' THEN 'A'
        WHEN "ooa"."username" LIKE '%@b.com{MANAGEMENT}' THEN 'B'
        WHEN "ooa"."username" LIKE '%@u.com.do{MANAGEMENT}' THEN 'U'
        WHEN "ooa"."username" LIKE '%@c.com{MANAGEMENT}' THEN 'C'
        ELSE '@OTHER'
    END AS "domain"

这是结果示例:

日期 时间_桶 SLA 队列 状态 决策者 计划中 触摸 域名 oo.count oo.dcount ooa.count ooa.dcount
2024-03-11 01:30 全部 已决定 代理 预期 1D:代理人 B 1 1 1 1
2024-03-11 01:30 全部 已决定 代理 预期 1D:代理人 B 1 1 1 1
2024-03-11 01:30 全部 已决定 代理 预期 1D:代理人 B 1 1 1 1
2024-03-11 01:30 全部 已决定 代理 预期 1D:代理人 B 1 1 1 1
2024-03-11 01:30 商城 已决定 代理 预期 1D:代理人 B 1 1 1 1
2024-03-11 01:30 商城 已决定 代理 预期 1D:代理人 B 2 2 2 2
2024-03-11 01:30 商城 已决定 代理 预期 1D:代理人 B 1 1 1 1
2024-03-11 01:30 商城 已决定 代理 预期 1D:代理人 B 1 1 1 1
2024-03-11 01:30 商城 已决定 代理 预期 1D:代理人 B 1 1 1 1
2024-03-11 01:30 商城 已决定 代理 预计 1D:代理人 B 1 1 1 1
2024-03-11 01:30 商城 已决定 代理 预期 1D:代理人 B 1 1 1 1

它并没有像我希望的那样汇总所有记录。我希望得到这个:

日期 时间_桶 SLA 队列 状态 决策者 计划中 触摸 域名 oo.count oo.dcount ooa.count ooa.dcount
2024-03-11 01:30 全部 已决定 代理 预期 1D:代理人 B 4 4 4 4
2024-03-11 01:30 商城 已决定 代理 预期 1D:代理人 B 8 8 8 8

在自己研究解决方案时,我发现最常见的错误是没有在“Group By”子句中使用正确的术语。我不认为这是我的问题,因为我使用基本上相同的“选择”和“分组依据”子句。

我有点迷失了,我开始相信这是由于执行顺序造成的,因为我没有其他解决方案。

sql amazon-redshift
1个回答
0
投票

尝试这样的查询:

SELECT
  CASE WHEN … END AS A,
  CASE WHEN … END AS B,
  count(1)
FROM mytable
GROUP BY 1, 2

1, 2 这里表示 select 语句中的列号 - 在本例中计算列 A, B。

© www.soinside.com 2019 - 2024. All rights reserved.