Can I use non-aggregate columns with group by?

前端 未结 6 806
不思量自难忘°
不思量自难忘° 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:13

    I think it's tempting indeed to ask the system to solve the problem in one pass rather than having to do the job twice (find the max, and the find the corresponding id). You can do using CONCAT (as suggested in Naktibalda refered article), not sure that would be more effeciant

    SELECT MAX( CONCAT( LPAD(age, 10, '0'), '-', id)
    FROM STUFF1
    GROUP BY kind;
    

    Should work, you have to split the answer to get the age and the id. (That's really ugly though)

提交回复
热议问题