这个PL / SQL语句有什么问题?

时间:2011-05-18 13:39:58

标签: sql oracle select ora-00979

我正在使用PL / SQL(Oracle)。 我以前写过查询,但我不确定这个查询有什么问题。我得到的错误是:

  

[错误]执行(942:41):   ORA-00979:不是GROUP BY表达式。

突出显示的文本是:case语句中的第一个TRIM函数。我知道我不能将这个case语句放在Group By子句中,因为我在这里聚合(使用sum函数)。有什么我不理解的吗?

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 ; 

3 个答案:

答案 0 :(得分:4)

您的问题是您尝试在分组上下文中访问非aggragate(LOBDESC)。您有两种选择,选择最有意义的选项:

  • LOBDESC添加到GROUP BY
  • 使用聚合函数,即MAX(LOBDESC)

答案 1 :(得分:2)

使用Oracle,在分组时,您只能选择:

  • GROUP BY子句中出现的字段
  • 在其他字段上汇总函数(COUNTSUM等)

您的CASE子句将被重写。也许,为了您的目的使用分析函数会更好:

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

答案 2 :(得分:2)

您必须将LOBDESC添加到GROUP BY列列表中,因为您在聚合函数外的select中使用它。