how to display a column without adding to group by

gal mor 141 Reputation points
2022-12-07T08:38:04.057+00:00

hello.
I have the following query:

       SELECT CASE GROUPING_ID(status, CASE WHEN status IN(2, 4, 5) THEN 1 ELSE 0 END)    
       WHEN 0 THEN TO_CHAR(status)    
       WHEN 2 THEN 'sum statuses 2,4,5'    
       ELSE 'sum'  
       END AS status,    
       case   
       when status = 0 then 'page was closed'   
       when status = 2 then 'isnt card owner'    
       when status = 3 then 'error'   
       when status = 4 then 'charge'   
       when status = 5 then 'got report, charge failed'    
       end as "description" ,  
       COUNT(CASE user_type WHEN 1 THEN 1 END) AS "1",   
       COUNT(CASE user_type WHEN 2 THEN 1 END) AS "2",    
       COUNT(CASE user_type WHEN 3 THEN 1 END) AS "3",    
       COUNT(CASE user_type WHEN 5 THEN 1 END) AS "5",    
       COUNT(*) AS total       
       FROM TRANSACTIONS  WHERE status != 1 AND Update_Date  BETWEEN TO_DATE('2022-01-04', 'yyyy-mm-dd HH24:MI:SS')  AND TO_DATE('2022-12-07', 'yyyy-mm-dd HH24:MI:SS')     
       GROUP BY CUBE(status, CASE WHEN status IN(2, 4, 5) THEN 1 ELSE 0 END)    
       HAVING GROUPING_ID(status, CASE WHEN status IN (2, 4, 5) THEN 1 ELSE 0 END) IN(0, 3)    
       OR(GROUPING_ID(status, CASE WHEN status IN(2, 4, 5) THEN 1 ELSE 0 END) = 2    AND CASE WHEN status IN(2, 4, 5) THEN 1 ELSE 0 END = 1)   

which displays:
268038-table.png

I want to remove that part from code:

  case   
  when status = 0 then 'page was closed'   
  when status = 2 then 'isnt card owner'    
  when status = 3 then 'error'   
  when status = 4 then 'charge'   
  when status = 5 then 'got report, charge failed'    
  end as "description" ,  

Originally the "DESCRIPTION" column is found in a different table called TRANSACTION_STATUS_CODES.
I want to change my FROM statement: FROM TRANSACTIONS to FROM TRANSACTIONS, TRANSACTION_STATUS_CODES and then to select the TRANSACTION_STATUS_CODES.DESCRIPTION
so far so good. when doing so (because Im using aggregate functions in my select statement, I cannot randomally add a column to the result set without having to include it in the GROUPBY clause. which makes it hard for me to catch up on how to do that...

Maybe this is a partition by's job?
but Im not sure and have no clue how to implement this in my query, any suggestions?
thanks !

Community Center | Not monitored
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 129.1K Reputation points MVP Volunteer Moderator
    2022-12-08T21:55:23.917+00:00

    Try replacing

       GROUP BY CUBE(status, CASE WHEN status IN(2, 4, 5) THEN 1 ELSE 0 END)  
    

    with

       GROUP BY CUBE(TRANSACTION_STATUS_CODES.DESCRIPTION, CASE WHEN TRANSACTIONS.status IN(2, 4, 5) THEN 1 ELSE 0 END)  
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.