Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order values inside group by

Consider the following SQL Server table:

  ID  |   X   |   Y
------+-------+-------
   1  |   1   |   1
   2  |   1   |   2
   3  |   1   |   3
   4  |   2   |   40
   5  |   2   |   500
   6  |   3   |   1
   7  |   3   |   100
   8  |   3   |   10

I need to select the ID of the row that has the maximum value of Y grouped by x, i.e:

  ID  |   X   |   Y
------+-------+-------
   3  |   1   |   3
   5  |   2   |  500
   7  |   3   |  100

The query will be nested several times so an optimal performance solution is required...

like image 847
Lu4 Avatar asked Jun 22 '11 01:06

Lu4


People also ask

How do I Group by store and sort by sales values?

#group by store and sort by sales values in ascending order df.sort_values( ['store','sales']).groupby('store').head() store sales 2 A 8 3 A 14 6 A 30 7 A 30 4 B 10 0 B 12 5 B 20 1 B 25 Note that the head () function only displays the first 5 values by group. To display the top n values by group, simply use head (n) instead.

What is the difference between group by and order by?

Only rows where the salesperson is not Bennett are considered. Use the ORDER BY clause to display the output table of a query in either ascending or descending alphabetical order. Whereas the GROUP BY clause gathers rows into groups and sorts the groups into alphabetical order, ORDER BY sorts individual rows.

How to group rows by store and sort in descending order?

We can use the following syntax to group the rows by the store column and sort in descending order based on the sales column: #group by store and sort by sales values in descending order df.sort_values( ['store','sales'],ascending=False).groupby('store').head() store sales 1 B 25 5 B 20 0 B 12 4 B 10 6 A 30 7 A 30 3 A 14 2 A 8

How do you sort a group by in a Dataframe?

Sort within Groups of groupby () Result in DataFrame By using DataFrame.sort_values (), you can sort DataFrame in ascending or descending order, before you use this first group the DataFrame rows by using DataFrame.groupby () method. Note that groupby preserves the order of rows within each group.


2 Answers

Setup:

 declare @MyTable table(ID int, X int, Y int)

 insert @MyTable
 values
    (   1  ,   1   ,   1),
    (   2  ,   1   ,   2),
    (   3  ,   1   ,   3),
    (   4  ,   2   ,   40),
    (   5  ,   2   ,   500),
    (   6  ,   3   ,   1),
    (   7  ,   3   ,   100),
    (   8  ,   3   ,   10)

Query:

;with cte
as
(
    select *, row_number() over(partition by X order by Y desc) RowNumber
    from @MyTable
)
select Id, X, Y
from cte
where RowNumber = 1

Result:

Id          X           Y
----------- ----------- -----------
3           1           3
5           2           500
7           3           100

The query is for MS SQL 2005+. The setup will work in MS SQL 2008+.

like image 59
Alex Aza Avatar answered Oct 18 '22 20:10

Alex Aza


Yeah, many times this type of question... Added keyword "STRAIGHT_JOIN" to pre-optimize the query by enforcing the "PreQuery" first.

select STRAIGHT_JOIN
      YT.*
   from 
      ( select x, max(y) HighPerX
           from YourTable 
           group by x ) PreQuery
      join
         YourTable YT
            on PreQuery.X = YT.X 
            AND PreQuery.HighPerX = YT.y
like image 37
DRapp Avatar answered Oct 18 '22 19:10

DRapp