这个 PL/SQL 语句有什么问题?
我正在使用 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 ;
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 ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题是您正在尝试访问分组上下文中的非聚合(
LOBDESC
)。您有两个选项,选择最有意义的一个:LOBDESC
添加到GROUP BY
MAX(LOBDESC)
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:LOBDESC
toGROUP BY
MAX(LOBDESC)
使用 Oracle,分组时,您只能选择:
GROUP BY
子句中出现的字段COUNT
、SUM
等)其他字段您的
CASE
子句将被重写。也许,使用分析函数来达到您的目的会更好:http://psoug.org/reference/ analytic_functions.html
With Oracle, when grouping, you can only select either:
GROUP BY
clauseCOUNT
,SUM
, etc) on other fieldsYour
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
您必须将
LOBDESC
添加到GROUP BY
列列表中,因为您在聚合函数外部的select
中使用它。You have to add
LOBDESC
to theGROUP BY
columns list because you're using it in theselect
outside of an agregation function.