I have a table with several columns:
id | Source | Mode | SponsorID | Code | ...
1 source1 Paper 123 7102
2 source2 Paper 123 7102
3 source2 Paper 123 7101
4 source1 Paper 123 7101
5 source2 Paper 123 1010
6 source1 Paper 345 1010
7 source2 Paper 345 7102
8 source1 Paper 345 1010
9 source2 Paper 345 7102
10 source1 Paper 345 7102
11 source1 Paper 678 1010
12 source2 Paper 678 1010
13 source1 Paper 678 1010
14 source2 Paper 678 1010
15 source1 Paper 678 1010
I want to group the above records by SponsorID
and only want to pick one record based on the value in code
column.
In the SponsorID
group I only want to pick the record that has code 7101. If that value does not exist in the code column I want to be able to pick the record with value 7102 in the code column. If 7102 does not exist I want to pick the record with 1010. So the final output should look something like:
1 source1 Paper 123 7101
2 source2 Paper 345 7102
3 source1 Paper 678 1010
I have tried using partitions and case but no success.
Would greatly appreciate if someone could help.