【问题标题】:Error :Group function not allowed错误:不允许组功能
【发布时间】:2018-11-13 03:11:45
【问题描述】:
select shipment_stop_d.shipment_gid,count(s_ship_unit_line.s_ship_unit_gid) 
from s_ship_unit_line,shipment_stop_d 
where shipment_stop_d.shipment_gid is not null 
and count(s_ship_unit_line.s_ship_unit_gid) > 200 
group by s_ship_unit_line.s_ship_unit_gid;

我需要的条件是在表s_ship_unit_line 中有多少个{(s_ship_unit_line.s_ship_unit_gid)} 的计数超过200 对于表shipment_gid 在表shipment_stop_d 中的shipment_gid

【问题讨论】:

  • s_ship_unit_lineshipment_stop_d是什么关系?如所写,此查询会将s_ship_unit_line 中的每一行连接到shipment_stop_d 中的每一行,即笛卡尔连接。 ???

标签: sql oracle oracle11g group-by aggregate-functions


【解决方案1】:

你在 where 子句中有count(s_ship_unit_line.s_ship_unit_gid) > 200,这是违反规则的。

count 是一个可以允许的组函数 在 select 列表或 have by 子句中。

使用

select d.shipment_gid,count(l.s_ship_unit_gid) 
  from s_ship_unit_line l join shipment_stop_d d 
    on ( d.id = l.shipment_id ) -- I assumed this columns for joining condition.
 where d.shipment_gid is not null 
 group by l.s_ship_unit_gid
having count(l.s_ship_unit_gid) > 200;

改为。

【讨论】:

  • ok 我使用了这个命令 select distinct ssd.shipment_gid,count(sul.s_ship_unit_gid) as line_number from s_ship_unit_line sul join shipping_stop_d ssd on sul.s_ship_unit_gid = ssd.s_ship_unit_gid where ssd.shipment_gid is not null group by ssd.shipment_gid 计数(sul.s_ship_unit_gid)>200;但问题是行号值是现有值的两倍。不知道为什么要乘以 2。
  • @ankitsingh 首先,不用distinct,已经有group by条件了。我想知道连接条件中的列 sul.s_ship_unit_gidssd.s_ship_unit_gid 分别在他们的表中的主键或唯一键?
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多