【问题标题】:unable to group the data in ORACLE with GROUP BY clause无法使用 GROUP BY 子句对 ORACLE 中的数据进行分组
【发布时间】:2013-12-28 10:44:28
【问题描述】:
select offices.office_id,
count(staff_id)
from offices join  staffs using (office_id) join managers using (manager_id)
group by office_id;

我一直在努力

第 1 行出现错误:ORA-25154:USING 子句的列部分不能有限定符。

我正在尝试计算不同办公室的员工总数。在这里,我按办公室 ID 对结果进行了分组。这里出了什么问题。

但如果我喜欢这样

select count(staff_id)
from offices join  staffs using (office_id) join managers using (manager_id)
group by office_id;

因为我删除了offices.office_id column,它以这种方式工作并且只显示一列。但是我需要在两列中并排放置相应数量的员工的办公室 ID。

【问题讨论】:

  • 按office.office_id使用组

标签: sql oracle join group-by


【解决方案1】:

在查询中使用USING 子句时,指定表名是多余的。只有列名是数据库的足够信息。例如 - select office_id。试试这个:

select office_id,
count(staff_id)
from offices join  staffs using (office_id) join managers using (manager_id)
group by office_id;

参考这个类似的问题:Cannot have a qualifier in the select list while performing a JOIN w/ USING keyword

【讨论】:

  • +1 Works,虽然 USING 现在应该被转换为 ON 语法。
  • @JoachimIsaksson。感谢 SQL Fiddle 链接。 :)
  • 这行得通。您说查询时指定表名是多余的。这几乎向我解释了一切。谢谢
【解决方案2】:

office_id 是跨表的公共列。所以你必须指定别名或使用 tablename.columnname

试试这个..

select offices.office_id,
count(staffs.staff_id)
from offices join  staffs on staffs.office_id=offices.office_id
join managers on managers.manager_id=staffs.manager_id
group by offices.office_id;

【讨论】:

  • 这不适用于USING 语法,只能用于ON
  • 我已将查询更改为 ON,这是 ANSI 标准
  • @user2760858 :查看最新编辑的答案。您应该确保根据您的表 ddl 连接列是正确的
猜你喜欢
  • 2014-10-22
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-28
  • 2015-07-03
  • 2020-08-19
  • 2021-04-06
相关资源
最近更新 更多