I have a table called TableA.
Input:
ColumnA ColumnB ColumnC
jim 1788 5F
jim 2000 9F
jim 500 9F
ben 190 4H
geri 40 manny
geri 40 kelly
geri 20 joker
geri 20 jam
Output:
ColumnA ColumnB ColumnC
jim 2000 9F
jim 2000 NULL
ben 190 4H
geri 40 manny
geri 40 kelly
geri 40 NULL
Can someone help me with the SQL query?
The logic is the following:
I want to group the rows by ColumnA, e.g. the “gerri” group will have 4 rows. For each group, the maximum element from ColumnB is calculated , for gerri group it’s 40.
Each element in the group is analysed for ColumnB: if the element.ColumnB = maximum, the row is put in the output (if it doesn’t exist already). Otherwise, if the element.ColumnB different than the maximum, the current row is put in the output with NULL on ColumnC and MAXIMUM on ColumnB (again, if it doesn’t exist in the output already).
It’s clearer from the example I gave.
Thank you for any suggestions in advance!
Example on Sql Fiddle.