使用 Group By 查询汇总统计信息
我一直在浏览网络,但没有找到解决我的问题的答案。我发现的最接近的是这个也来自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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您很高兴在整个页面上看到这些摘要,最简单的方法是:
编辑:如果您需要在页面上查看结果,请尝试:
Assuming you are happy to see these summaries across the page, the simplest way to do this would be:
EDIT: If you need to see the results down the page, try:
编辑:尝试仅稍微更改您的解码:
STATUS_CD 2 和 3 现在生成相同的文本“Status_C_D”,并将在相应的数据透视表中生成单个列。
(我的第一个代码示例是错误的,因为 STATUS_CD 是一个数字,您可以将其转换为文本。)
EDIT: Try by only changing your DECODE slightly:
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.)