如何显示在 sql 中被组删除的列
how to show a column thats removed by a group by in sql
请看下面我的查询。我如何将它设置为 return 具有最大年龄的 1 个子对象。如果我按子对象分组,那么它会显示所有子对象
select
object,
max(Age) as Age,
from table
group by 1,
您可以使用解析函数(MAX OVER
):
select *
from
(
select t.*, max(age) over (partition by object) as max_age
from table t
) with_max_age
where age = max_age
order by object;
试试下面的方法
;with cte as
(
select Object,SubObject,ROW_NUMBER() over (partition by Object order by age desc) as rowNum
from table
)
select * from cte where rowNum=1
请看下面我的查询。我如何将它设置为 return 具有最大年龄的 1 个子对象。如果我按子对象分组,那么它会显示所有子对象
select
object,
max(Age) as Age,
from table
group by 1,
您可以使用解析函数(MAX OVER
):
select *
from
(
select t.*, max(age) over (partition by object) as max_age
from table t
) with_max_age
where age = max_age
order by object;
试试下面的方法
;with cte as
(
select Object,SubObject,ROW_NUMBER() over (partition by Object order by age desc) as rowNum
from table
)
select * from cte where rowNum=1