‘test_table’ contains a column called vendor, with char values from 1 to 9, 1 being highest priority. ‘test_table’ also has another column named match, with char values either ‘I’ or ‘H’, with ‘I’ receiving higher priority. I want to return all rows with unique values in ID, prioritized by match, then vendor.
Test_Table
ID Vendor Match
1 3 I
1 2 I
1 4 H
2 1 H
2 1 I
3 1 H
3 2 I
Results Desired
ID Vendor Match
1 2 I
2 1 I
3 2 I
SELECT *
FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS RowNo, *
FROM test_table) x
WHERE RowNo = 1
ORDER BY ID
Any help would be greatly appreciated. Thank you!
You need to have an order by clause for match column ( character comparison happens based on ascii value) and another one on vendor column . You can even remove
asciikeyowrd ( for the sake of clarity i have written) and simply use the column name in order by clauseTry this :