我一直在网上看,但没有找到我的问题的答案。我发现的最接近的是来自StackOverflow的this。
所以我所拥有的是具有不同状态的交易。我正在尝试编译一个查询,根据其状态汇总所有事务。我可以通过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
答案 0 :(得分:0)
假设您很高兴在整个页面上看到这些摘要,最简单的方法是:
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"
答案 1 :(得分:0)
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
编辑:尝试仅稍微更改您的DECODE:
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是一个数字,您将其转换为文本。)