Oracle - 在 having 子句中分组
Oracle - group by in having clause
我的查询如下所示
select f.entity, MAX(to_char(f.whencreated, 'MM/DD/YYYY HH24:MI:SS')) from fan f
group by f.entity
having MAX((f.whencreated)) >
(select MAX((b.endtime)) from brun b
where b.cny# = f.cny#
and b.entity = f.entity
group by b.entity, f.entity);
我收到错误
ORA-00979: not a GROUP BY expression
在此查询中,如果 table f 中该实体的 Max(whencreated) 大于 MAX((b.endtime,我想 select f.entity )) table brun.
中的同一实体
table 如下所示:
Table粉丝:
ENTITY WHENCREATED
A 09/01/2020 12:34:00
A 10/01/2020 12:12:12
B 08/01/2020 12:34:00
B 10/01/2020 12:12:12
Table 燃烧:
ENTITY ENDTIME
A 09/02/2020 12:34:00
A 09/04/2020 12:12:12
B 08/01/2020 12:34:00
B 11/01/2020 12:12:12
查询应该return
A 10/01/2020 12:12:12
因为实体 A 的最大值 (brun.endtime) 是 09/04/2020 12:12:12,小于实体 A 的最大值 (fan.whencreated),后者是 10/01/2020 12:12:12.
我认为错误的原因是您在 HAVING
子句中的子查询引用了 f.cny#
。由于该列不在主查询的 GROUP BY
子句中,因此此时无法引用它。
我认为您需要阐明您要实现的目标。 “同一实体”意味着 entity
列的值相同,仅此而已。
我会尝试不同的方法:
with temp as
(select
f.entity,
max(f.whencreated) max_fwhen,
max(b.endtime) max_bend
from fan f join brun b on b.dny# = f.cny#
and b.entity = f.entity
group by f.entity
)
select entity
from temp
where max_fwhen > max_bend;
顺便说一句,不要 MAX 一个字符串;我相信您想使用日期,而不是字符串。你会得到意想不到的结果,例如1920 年 8 月 25 日比 2021 年 2 月 12 日“更大”。
select f.entity if the Max(whencreated) of that entity in table f is more than the MAX((b.endtime))
好吧,加入之前聚合一下如何:
select f.entity
from (select f.entity, max(whencreated)as maxwc
from fan f
group by f.entity
) f join
(select b.entity, max(b.endtime) as maxet
from brun b
group by b.entity
) b
on f.entity = b.entity and f.maxwc > b.maxet
我的查询如下所示
select f.entity, MAX(to_char(f.whencreated, 'MM/DD/YYYY HH24:MI:SS')) from fan f
group by f.entity
having MAX((f.whencreated)) >
(select MAX((b.endtime)) from brun b
where b.cny# = f.cny#
and b.entity = f.entity
group by b.entity, f.entity);
我收到错误
ORA-00979: not a GROUP BY expression
在此查询中,如果 table f 中该实体的 Max(whencreated) 大于 MAX((b.endtime,我想 select f.entity )) table brun.
中的同一实体table 如下所示:
Table粉丝:
ENTITY WHENCREATED
A 09/01/2020 12:34:00
A 10/01/2020 12:12:12
B 08/01/2020 12:34:00
B 10/01/2020 12:12:12
Table 燃烧:
ENTITY ENDTIME
A 09/02/2020 12:34:00
A 09/04/2020 12:12:12
B 08/01/2020 12:34:00
B 11/01/2020 12:12:12
查询应该return
A 10/01/2020 12:12:12
因为实体 A 的最大值 (brun.endtime) 是 09/04/2020 12:12:12,小于实体 A 的最大值 (fan.whencreated),后者是 10/01/2020 12:12:12.
我认为错误的原因是您在 HAVING
子句中的子查询引用了 f.cny#
。由于该列不在主查询的 GROUP BY
子句中,因此此时无法引用它。
我认为您需要阐明您要实现的目标。 “同一实体”意味着 entity
列的值相同,仅此而已。
我会尝试不同的方法:
with temp as
(select
f.entity,
max(f.whencreated) max_fwhen,
max(b.endtime) max_bend
from fan f join brun b on b.dny# = f.cny#
and b.entity = f.entity
group by f.entity
)
select entity
from temp
where max_fwhen > max_bend;
顺便说一句,不要 MAX 一个字符串;我相信您想使用日期,而不是字符串。你会得到意想不到的结果,例如1920 年 8 月 25 日比 2021 年 2 月 12 日“更大”。
select f.entity if the Max(whencreated) of that entity in table f is more than the MAX((b.endtime))
好吧,加入之前聚合一下如何:
select f.entity
from (select f.entity, max(whencreated)as maxwc
from fan f
group by f.entity
) f join
(select b.entity, max(b.endtime) as maxet
from brun b
group by b.entity
) b
on f.entity = b.entity and f.maxwc > b.maxet