I have this query :
select pivot_table.*
from (
Select STATUS,USER_TYPE
FROM TRANSACTIONS tr
join TRANSACTION_STATUS_CODES sc on sc.id = tr.user_type
join TRANSACTION_USER_TYPES ut on ut.id=tr.user_type
WHERE Tr.User_Type between 1 and 5
And tr.status!=1
AND Tr.Update_Date BETWEEN TO_DATE('2022-01-01 00:00:00', 'yyyy-mm-dd HH24:MI:SS')
AND TO_DATE('2022-11-13 23:59:59', 'yyyy-mm-dd HH24:MI:SS')
) t
pivot(
count(user_type)
FOR user_type IN (1,2,3,5)
) pivot_table;
Which gives:
status | 1 | 2 | 3 | 5 |
---|---|---|---|---|
2 | 3 | 0 | 0 | 0 |
4 | 13 | 0 | 0 | 0 |
5 | 1 | 0 | 0 | 0 |
3 | 5 | 0 | 0 | 1 |
0 | 4 | 0 | 0 | 8 |
Wanted result:
status | 1 | 2 | 3 | 5 | total |
---|---|---|---|---|---|
2 | 3 | 0 | 0 | 0 | 3 |
4 | 13 | 0 | 0 | 0 | 13 |
5 | 1 | 0 | 0 | 0 | 1 |
3 | 5 | 0 | 0 | 1 | 6 |
0 | 4 | 0 | 0 | 8 | 12 |
sum of statuses 2,4,5 | 17 | 0 | 0 | 0 | 17 |
sum of all statuses | 26 | 0 | 0 | 0 | 35 |
I have tried adding:
Select STATUS,USER_TYPE,
count(user_type) as records,
sum(user_type) over (partition by status) as total
and in the end:
pivot ( sum (records) for user_type in (1,2,3,5)) pivot_table
but logically I am still not there.