group by 和 order by 的组合在oracle中得到结果
combination of group by and order by to get the results in oracle
我有一个 table,其中包含 departmentid、位置、DOJ、sal 和年龄等详细信息。
我想知道 depid 的 max sal、max age、depid 和位置组 :
我编写了以下查询,但未能在 table 上执行。
SELECT depid,location,MAX(sal),MAX(age) FROM company GROUP BY depid ORDER BY doj DESC
错误:
ORA-00979: 不是 GROUP BY 表达式
Table 数据:
预期结果:
你有两种方法:
- 将
location
添加到 GROUP BY
子句。
- 在 select 中使用像
MAX(location)
这样的聚合函数。
你应该这样写:
SELECT
depid,
first_value(location)over(partition by depid order by doj desc) location ,
MAX(sal),
MAX(age)
FROM company GROUP BY depid ORDER BY doj DESC
我有一个 table,其中包含 departmentid、位置、DOJ、sal 和年龄等详细信息。
我想知道 depid 的 max sal、max age、depid 和位置组 :
我编写了以下查询,但未能在 table 上执行。
SELECT depid,location,MAX(sal),MAX(age) FROM company GROUP BY depid ORDER BY doj DESC
错误: ORA-00979: 不是 GROUP BY 表达式
Table 数据:
预期结果:
你有两种方法:
- 将
location
添加到GROUP BY
子句。 - 在 select 中使用像
MAX(location)
这样的聚合函数。
你应该这样写:
SELECT
depid,
first_value(location)over(partition by depid order by doj desc) location ,
MAX(sal),
MAX(age)
FROM company GROUP BY depid ORDER BY doj DESC