0

Is grouping possible after sorting in MySQL? I read some articles regarding this, but none of the implementations were able to give me the result that I want.

My current data structure (after multiple joins) is

enter image description here

What I am willing to achieve is to group the data by pid where the data is sorted in desc order as per the created_at date.

pid   id   ....
3     16   ....
4     8   ....
2     6   ....
1     5   ....

Any idea on how this can be acheived?

prakashchhetri
  • 1,806
  • 4
  • 36
  • 61
  • What is wrong with sorting after grouping, where it makes logical sense to do a sort on the smallest data set – RiggsFolly Sep 01 '22 at 11:25
  • I am guessing your are making use of the MySQL fault [This is not actually allowed in "proper SQL." You are only allowed to select columns that are part of the grouping, or the result of using aggregate functions. MySQL has a more fault-tolerant mode, in which it allows for this - but the values of all other columns, that are not part of the grouping, would be those of one "random" record inside the group](https://stackoverflow.com/questions/25800411/mysql-isnt-in-group-by) – RiggsFolly Sep 01 '22 at 11:26
  • I am guessing you want one of the [tag:greatest-n-per-group] solutions. This type of question is asked daily on Stack Overflow, and there are many solutions posted already. – Bill Karwin Sep 01 '22 at 15:25

0 Answers0