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

发布于 2024-11-08 01:35:05 字数 1973 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

药祭#氼 2024-11-15 01:35:05

您的问题是您正在尝试访问分组上下文中的非聚合(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:

  • Add LOBDESC to GROUP BY
  • Use an aggregate function, i.e. MAX(LOBDESC)
所有深爱都是秘密 2024-11-15 01:35:05

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

  • GROUP BY 子句中出现的字段
  • 聚合函数(COUNTSUM 等)其他字段

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

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

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

墟烟 2024-11-15 01:35:05

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文