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