使用 Group By 查询汇总统计信息

发布于 2024-12-28 01:41:55 字数 1893 浏览 3 评论 0原文

我一直在浏览网络,但没有找到解决我的问题的答案。我发现的最接近的是这个也来自StackOverflow。

所以我拥有的是不同状态的交易。我正在尝试编译一个查询,该查询将根据状态汇总所有交易。我可以使用 Group By 轻松做到这一点,但我需要但不能做的是对两个特定状态进行求和。例如:

Status 
--------
Status_A
Status_B
Status_C_D
Status_C
Status_D
Status_E

上面的 Status_C_D 将是 Status_C 和 Status_D 的总和。如果您想知道,状态 C 是一种事务错误,而 Status_D 是另一种错误,因此他们希望两者的总和可以轻松查看有错误的事务总数(所有内容都将显示在报告上)。我所拥有的是:

SELECT DECODE(STATUS_CD, 
         0, 'Status_A',
         1, 'Status_B',
         2, 'Status_C',
         3, 'Status_D',
         4, 'Status_E',
         'Unknown') status
FROM table_a ...

希望这是足够的背景。非常感谢任何帮助。 :)

select decode(TABLE_A.status_cd, 0,  'Status A',
                                 1, 'Status B',
                                 2, 'Status C',
                                 3, 'Status D',
                                 4, 'Status E',
                                 'Unknown') status, 
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) +8) / 8), 1, 1, 0)) "0-7",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) +8) / 8), 2, 1, 0)) "8-15",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) +8) / 8), 3, 1, 0)) "16-23",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) +9) / 8), 4, 1, 0)) "24-30",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) -1) / 30), 1, 1, 0)) "31-60",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) -1) / 30), 2, 1, 0)) "61-90",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) -1) / 30), 3, 1, 0)) "91-120",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) -1) / 60), 2, 1, 0)) "121-180",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time)) / 181), 0, 0, 1)) ">180"
FROM Table_A
WHERE ...
GROUP BY TABLE_A.status_cd

I have been looking over the web but have not come across an answer to my problem. The closest I found was this also from StackOverflow.

So what I have is transactions with different statuses. I am trying to compile a query that will summarize all transactions according to their status. I can easily do that with a Group By, but what I need and can’t do is to sum two of the particular statuses. For example:

Status 
--------
Status_A
Status_B
Status_C_D
Status_C
Status_D
Status_E

Status_C_D above would be the sum of Status_C and Status_D. In case you are wondering, status C is a kind of transaction error, and Status_D is another kind of error, so they want the sum of both to easily see the total of transactions with errors (everything will be shown on a report). What I have is this:

SELECT DECODE(STATUS_CD, 
         0, 'Status_A',
         1, 'Status_B',
         2, 'Status_C',
         3, 'Status_D',
         4, 'Status_E',
         'Unknown') status
FROM table_a ...

Hopefully that's enough context. Any help is much appreciated. :)

select decode(TABLE_A.status_cd, 0,  'Status A',
                                 1, 'Status B',
                                 2, 'Status C',
                                 3, 'Status D',
                                 4, 'Status E',
                                 'Unknown') status, 
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) +8) / 8), 1, 1, 0)) "0-7",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) +8) / 8), 2, 1, 0)) "8-15",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) +8) / 8), 3, 1, 0)) "16-23",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) +9) / 8), 4, 1, 0)) "24-30",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) -1) / 30), 1, 1, 0)) "31-60",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) -1) / 30), 2, 1, 0)) "61-90",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) -1) / 30), 3, 1, 0)) "91-120",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time) -1) / 60), 2, 1, 0)) "121-180",
        SUM(decode(trunc(((sysdate - TABLE_A.date_time)) / 181), 0, 0, 1)) ">180"
FROM Table_A
WHERE ...
GROUP BY TABLE_A.status_cd

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任性一次 2025-01-04 01:41:55

假设您很高兴在整个页面上看到这些摘要,最简单的方法是:

select sum(DECODE(STATUS_CD, 0,1, 0)) Status_A,
       sum(DECODE(STATUS_CD, 1,1, 0)) Status_B,
       sum(DECODE(STATUS_CD, 2,1, 3,1, 0)) Status_C_D,
       sum(DECODE(STATUS_CD, 2,1, 0)) Status_C,
       sum(DECODE(STATUS_CD, 3,1, 0)) Status_D,
       sum(DECODE(STATUS_CD, 4,1, 0)) Status_E
FROM table_a ...

编辑:如果您需要在页面上查看结果,请尝试:

select "status", 
        SUM(decode(trunc(((sysdate - "date_time") +8) / 8), 1, 1, 0)) "0-7",
        SUM(decode(trunc(((sysdate - "date_time") +8) / 8), 2, 1, 0)) "8-15",
        SUM(decode(trunc(((sysdate - "date_time") +8) / 8), 3, 1, 0)) "16-23",
        SUM(decode(trunc(((sysdate - "date_time") +9) / 8), 4, 1, 0)) "24-30",
        SUM(decode(trunc(((sysdate - "date_time") -1) / 30), 1, 1, 0)) "31-60",
        SUM(decode(trunc(((sysdate - "date_time") -1) / 30), 2, 1, 0)) "61-90",
        SUM(decode(trunc(((sysdate - "date_time") -1) / 30), 3, 1, 0)) "91-120",
        SUM(decode(trunc(((sysdate - "date_time") -1) / 60), 2, 1, 0)) "121-180",
        SUM(decode(trunc(((sysdate - "date_time")) / 181), 0, 0, 1)) ">180"
from
(SELECT DECODE(coalesce(d.dummy_status, a.STATUS_CD), 
         'X', 'Status_C_D'
         '0', 'Status_A',
         '1', 'Status_B',
         '2', 'Status_C',
         '3', 'Status_D',
         '4', 'Status_E',
         'Unknown') "status",
         a."date_time"
 FROM table_a a
 LEFT JOIN (select '2' status_cd, '2' dummy_status from dual union
            select '2' status_cd, 'X' dummy_status from dual union
            select '3' status_cd, '3' dummy_status from dual union
            select '3' status_cd, 'X' dummy_status from dual) d
        ON a.status_cd = d.status_cd
 ...
) group by "status"

Assuming you are happy to see these summaries across the page, the simplest way to do this would be:

select sum(DECODE(STATUS_CD, 0,1, 0)) Status_A,
       sum(DECODE(STATUS_CD, 1,1, 0)) Status_B,
       sum(DECODE(STATUS_CD, 2,1, 3,1, 0)) Status_C_D,
       sum(DECODE(STATUS_CD, 2,1, 0)) Status_C,
       sum(DECODE(STATUS_CD, 3,1, 0)) Status_D,
       sum(DECODE(STATUS_CD, 4,1, 0)) Status_E
FROM table_a ...

EDIT: If you need to see the results down the page, try:

select "status", 
        SUM(decode(trunc(((sysdate - "date_time") +8) / 8), 1, 1, 0)) "0-7",
        SUM(decode(trunc(((sysdate - "date_time") +8) / 8), 2, 1, 0)) "8-15",
        SUM(decode(trunc(((sysdate - "date_time") +8) / 8), 3, 1, 0)) "16-23",
        SUM(decode(trunc(((sysdate - "date_time") +9) / 8), 4, 1, 0)) "24-30",
        SUM(decode(trunc(((sysdate - "date_time") -1) / 30), 1, 1, 0)) "31-60",
        SUM(decode(trunc(((sysdate - "date_time") -1) / 30), 2, 1, 0)) "61-90",
        SUM(decode(trunc(((sysdate - "date_time") -1) / 30), 3, 1, 0)) "91-120",
        SUM(decode(trunc(((sysdate - "date_time") -1) / 60), 2, 1, 0)) "121-180",
        SUM(decode(trunc(((sysdate - "date_time")) / 181), 0, 0, 1)) ">180"
from
(SELECT DECODE(coalesce(d.dummy_status, a.STATUS_CD), 
         'X', 'Status_C_D'
         '0', 'Status_A',
         '1', 'Status_B',
         '2', 'Status_C',
         '3', 'Status_D',
         '4', 'Status_E',
         'Unknown') "status",
         a."date_time"
 FROM table_a a
 LEFT JOIN (select '2' status_cd, '2' dummy_status from dual union
            select '2' status_cd, 'X' dummy_status from dual union
            select '3' status_cd, '3' dummy_status from dual union
            select '3' status_cd, 'X' dummy_status from dual) d
        ON a.status_cd = d.status_cd
 ...
) group by "status"
梦幻的心爱 2025-01-04 01:41:55
SELECT Status, SUM(other_column) AS other_sum
FROM
    (SELECT
        CASE WHEN STATUS_CD = 'Status_C' OR STATUS_CD = 'Status_D'
            THEN 'Status_C_D'
            ELSE STATUS_CD
        END AS Status,
        other_column
    FROM
        table_a) A
GROUP BY
    Status

编辑:尝试仅稍微更改您的解码:

SELECT status, SUM(other_column) AS other_sum
FROM
    (SELECT
        DECODE(STATUS_CD,  
               0, 'Status_A', 
               1, 'Status_B', 
               2, 'Status_C_D', 
               3, 'Status_C_D', 
               4, 'Status_E', 
               'Unknown') status, 
        other_column
    FROM
        table_a) A
GROUP BY
    status

STATUS_CD 2 和 3 现在生成相同的文本“Status_C_D”,并将在相应的数据透视表中生成单个列。

(我的第一个代码示例是错误的,因为 STATUS_CD 是一个数字,您可以将其转换为文本。)

SELECT Status, SUM(other_column) AS other_sum
FROM
    (SELECT
        CASE WHEN STATUS_CD = 'Status_C' OR STATUS_CD = 'Status_D'
            THEN 'Status_C_D'
            ELSE STATUS_CD
        END AS Status,
        other_column
    FROM
        table_a) A
GROUP BY
    Status

EDIT: Try by only changing your DECODE slightly:

SELECT status, SUM(other_column) AS other_sum
FROM
    (SELECT
        DECODE(STATUS_CD,  
               0, 'Status_A', 
               1, 'Status_B', 
               2, 'Status_C_D', 
               3, 'Status_C_D', 
               4, 'Status_E', 
               'Unknown') status, 
        other_column
    FROM
        table_a) A
GROUP BY
    status

STATUS_CD 2 and 3 now produce the same text 'Status_C_D' and will result in a single column in a corresponding pivot table.

(My first code exmample was wrong, since STATUS_CD is a number, which you convert to a text.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文