Can I use non-aggregate columns with group by?

前端 未结 6 716
不思量自难忘°
不思量自难忘° 2020-12-09 09:24

You cannot (should not) put non-aggregates in the SELECT line of a GROUP BY query.

I would however like access the one of the non-aggregate

6条回答
  •  有刺的猬
    2020-12-09 10:10

    In recent databases you can use sum() over (parition by ...) to solve this problem:

    select id, kind, age as max_age from (
      select id, kind, age, max(age) over (partition by kind) as mage
        from table)
    where age = mage
    

    This can then be single pass

提交回复
热议问题