ORA-00937: 不是单组函数 变通方法
ORA-00937: not a single-group group function Workaround
select
o.customer_id,
c.cust_first_name,
c.cust_last_name,
c.income_level,
to_char(o.order_date, 'DD-MON-YY HH12:MI') as order_date,
ROW_NUMBER() over (partition by o.customer_id order by o.order_date) as order#,
o.order_total,
lag(o.order_total, 1, 0) over (partition by o.customer_id order by o.customer_id)
+ lag(o.order_total, 2, 0) over (partition by o.customer_id order by o.customer_id) as last_two_orders_sum,
min(o.order_date) keep (dense_rank last order by o.customer_id) as first_order_total
from orders o, customers c
where o.customer_id = c.customer_id
这是我产生此错误的查询:
ORA-00937: not a single-group group function
最后一列的预期结果 - 每个客户的第一个订单:
我知道我必须使用 GROUP BY 子句来处理这个错误,但按所有其他列分组似乎不太合适。我知道这是 Oracle 11g 中的一个限制,这个问题已在12c,但我仍然需要某种解决方法。
尝试过:
CTE --> 在 CTE
中有一个 select 有点奇怪
子查询 --> 无法找出正确的语法,结果一团糟
感谢任何帮助!
而不是 MIN(...) KEEP (...)
聚合函数,您想要使用 MIN(...) KEEP (...) OVER (...)
分析函数,或者,可能没有 KEEP
,使用 MIN(...) OVER (PARTITION BY ...)
:
select
o.customer_id,
c.cust_first_name,
c.cust_last_name,
c.income_level,
to_char(o.order_date, 'DD-MON-YY HH12:MI') as order_date,
ROW_NUMBER() over (partition by o.customer_id order by o.order_date)
as order#,
o.order_total,
lag(o.order_total, 1, 0)
over (partition by o.customer_id order by o.customer_id)
+ lag(o.order_total, 2, 0)
over (partition by o.customer_id order by o.customer_id)
as last_two_orders_sum,
MIN(o.order_date) OVER (PARTITION BY o.customer_id) as first_order_date,
MIN(o.order_total) KEEP (DENSE_RANK FIRST ORDER BY o.order_date)
OVER (PARTITION BY o.customer_id) as first_order_total
from orders o
INNER JOIN customers c
ON o.customer_id = c.customer_id
注意:另外,使用 ANSI 连接语法而不是旧的 comma-join 语法。
I know that this is a limitation in Oracle 11g and this issue is fixed in 12c, but I still need some kind of workaround.
不,这不是您可以通过升级到更高版本来解决的问题。您的查询的一个基本问题是您混合使用了聚合列和 non-aggregated 列,并且正如错误消息所暗示的那样,您没有包含所有 [=33] 的 GROUP BY
子句=]列。
解决方案是不使用任何聚合函数,改为仅使用分析函数。
db<>fiddle here
select
o.customer_id,
c.cust_first_name,
c.cust_last_name,
c.income_level,
to_char(o.order_date, 'DD-MON-YY HH12:MI') as order_date,
ROW_NUMBER() over (partition by o.customer_id order by o.order_date) as order#,
o.order_total,
lag(o.order_total, 1, 0) over (partition by o.customer_id order by o.customer_id)
+ lag(o.order_total, 2, 0) over (partition by o.customer_id order by o.customer_id) as last_two_orders_sum,
min(o.order_date) keep (dense_rank last order by o.customer_id) as first_order_total
from orders o, customers c
where o.customer_id = c.customer_id
这是我产生此错误的查询:
ORA-00937: not a single-group group function
最后一列的预期结果 - 每个客户的第一个订单:
我知道我必须使用 GROUP BY 子句来处理这个错误,但按所有其他列分组似乎不太合适。我知道这是 Oracle 11g 中的一个限制,这个问题已在12c,但我仍然需要某种解决方法。
尝试过:
CTE --> 在 CTE
中有一个 select 有点奇怪子查询 --> 无法找出正确的语法,结果一团糟
感谢任何帮助!
而不是 MIN(...) KEEP (...)
聚合函数,您想要使用 MIN(...) KEEP (...) OVER (...)
分析函数,或者,可能没有 KEEP
,使用 MIN(...) OVER (PARTITION BY ...)
:
select
o.customer_id,
c.cust_first_name,
c.cust_last_name,
c.income_level,
to_char(o.order_date, 'DD-MON-YY HH12:MI') as order_date,
ROW_NUMBER() over (partition by o.customer_id order by o.order_date)
as order#,
o.order_total,
lag(o.order_total, 1, 0)
over (partition by o.customer_id order by o.customer_id)
+ lag(o.order_total, 2, 0)
over (partition by o.customer_id order by o.customer_id)
as last_two_orders_sum,
MIN(o.order_date) OVER (PARTITION BY o.customer_id) as first_order_date,
MIN(o.order_total) KEEP (DENSE_RANK FIRST ORDER BY o.order_date)
OVER (PARTITION BY o.customer_id) as first_order_total
from orders o
INNER JOIN customers c
ON o.customer_id = c.customer_id
注意:另外,使用 ANSI 连接语法而不是旧的 comma-join 语法。
I know that this is a limitation in Oracle 11g and this issue is fixed in 12c, but I still need some kind of workaround.
不,这不是您可以通过升级到更高版本来解决的问题。您的查询的一个基本问题是您混合使用了聚合列和 non-aggregated 列,并且正如错误消息所暗示的那样,您没有包含所有 [=33] 的 GROUP BY
子句=]列。
解决方案是不使用任何聚合函数,改为仅使用分析函数。
db<>fiddle here