Question

I am working with PL/SQL (Oracle). I have written queries before, but I'm not sure what is wrong with this query. The error I am getting is this:

[Error] Execution (942: 41): ORA-00979: not a GROUP BY expression.

The highlighted text is: the first TRIM function in the case statement. I know that I can't put this case statement in the Group By clause because I am aggregating here (using the sum function). Is there something I am not understanding??

SELECT 
T.PLANNAME, 
M.CLASS, 
M.BRAND_OR_GENERIC, 
T.PROCEDURECODE, 
M.BRAND_NAME, 
M.GENERIC_NAME, 
CASE 
WHEN (TRIM(PLANNAME) = 'XXXX' AND TRIM(LOBDESC) = 'COMMERCIAL') THEN ROUND((SUM(ALLOWEDAMT)/160000)*1000000, 2)
WHEN (TRIM(PLANNAME) = 'XXXX' AND TRIM(LOBDESC) = 'MEDICARE') THEN ROUND((SUM(ALLOWEDAMT)/14000)*1000000, 2)
WHEN (TRIM(PLANNAME) = 'YYYY' AND TRIM(LOBDESC) = 'COMMERCIAL') THEN ROUND((SUM(ALLOWEDAMT)/1800000)*1000000, 2)
WHEN (TRIM(PLANNAME) = 'YYYY' AND TRIM(LOBDESC) = 'MEDICARE') THEN ROUND((SUM(ALLOWEDAMT)/35000)*1000000, 2)
WHEN (TRIM(PLANNAME) LIKE 'ZZZZ%' AND TRIM(LOBDESC) = 'COMMERCIAL') THEN ROUND((SUM(ALLOWEDAMT)/1200462)*1000000, 2)
WHEN (TRIM(PLANNAME) LIKE 'ZZZZ%' AND TRIM(LOBDESC) = 'MEDICARE') THEN ROUND((SUM(ALLOWEDAMT)/235000)*1000000, 2)
WHEN (TRIM(PLANNAME) = 'AAAA' AND TRIM(LOBDESC) = 'COMMERCIAL') THEN ROUND((SUM(ALLOWEDAMT)/200000)*1000000, 2)
WHEN (TRIM(PLANNAME) = 'BBBB' AND TRIM(LOBDESC) = 'MEDICAID') THEN ROUND((SUM(ALLOWEDAMT)/147000)*1000000, 2)
END As AllowedPerMM,
SUM(T.ALLOWEDAMT) As SumOfALLOWEDAMT 


FROM FIN.TR_2011 T
LEFT JOIN FIN.TR_REFERENCE M ON T.PROCEDURECODE = M.PROCEDURECODE

WHERE 
T.PROCEDURECODE IS NOT NULL AND  
(T.PROCEDURECODE <> '0' or T.PROCEDURECODE <> 0)  AND 

(T.PROCEDURECODE Like 'J%' OR 
T.PROCEDURECODE Like 'C9%' OR
T.PROCEDURECODE Like 'S0%' OR
T.PROCEDURECODE Like 'Q%' OR 
T.PROCEDURECODE = '90378' OR 
T.PROCEDURECODE IN ( 
'J9171', 'J9265', 'J9264', 
'J2430', 'J3487',
'J9000', 'J9001') OR
M.THERAPEUTIC_CLASS IN ('RA')
) AND 
TRIM(T.YEAR) IN ('2010')

GROUP BY 
T.PLANNAME, 
M.CLASS, 
M.BRAND_OR_GENERIC, 
T.PROCEDURECODE, 
M.BRAND_NAME, 
M.GENERIC_NAME

ORDER BY SumOfALLOWEDAMT DESC ; 
Was it helpful?

Solution

Your problem is that you are trying to access a non aggragate (LOBDESC) in a grouping context. You have two options, choose the one which makes most sense:

  • Add LOBDESC to GROUP BY
  • Use an aggregate function, i.e. MAX(LOBDESC)

OTHER TIPS

With Oracle, when grouping, you can only select either:

  • Fields appearing in the GROUP BY clause
  • Aggregate functions (COUNT, SUM, etc) on other fields

Your CASE clause will have be rewritten. Probably, you will be better off by using analytic functions for your purpose:

http://psoug.org/reference/analytic_functions.html

You have to add LOBDESC to the GROUP BY columns list because you're using it in the select outside of an agregation function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top