I need to write a query for a table with 5 columns which are ID1, ID2, ID3, A, and B. Here,
ID1, ID2, and ID3 form the primary key.
For each ID1, find maximum A. If two or more As have the maximum values, choose the only one record that has maximum B. Finally show the results with corresponding ID2 and ID3 values.
For example, for the following table
ID1 ID2 ID3 A B
1 2 3 10 5
1 3 4 30 4
1 3 5 30 3
2 2 3 5 1
2 3 4 9 2
2 3 5 11 3
the query should show the results as follows.
ID1 ID2 ID3 Max_A
1 3 4 30
2 3 5 11
The gist of this is to
ROW_NUMBERto each rowID1ORDERfrom high to low onAandB.CTEfor easy access onrnSQL Statement