Reputation: 69
I have a list of values that I need to have the query sort in a specific order.
ID | VALUE |
1 AA
1 OUT
2 OUT
2 OUT
3 AA
3 OUT
4 CC
4 OUT
4 OUT
5 CC
5 OUT
6 AA
6 OUT
7 CC
7 OUT
8 OUT
8 BB
I need the above to sort like this:
ID | VALUE |
1 AA
1 OUT
3 AA
3 OUT
6 AA
6 OUT
8 BB
8 OUT
4 CC
4 OUT
4 OUT
5 CC
5 OUT
7 CC
7 OUT
2 OUT
2 OUT
Any ideas on the best way to do this in SQL?
Thanks for your time in advance.
Upvotes: 0
Views: 43
Reputation: 35900
Just putting another idea:
Order by id,
case when value <> 'OUT' then value end
Cheers!!
Upvotes: 0
Reputation: 1269543
I think you want:
order by max(case when value <> 'OUT' then value end) over (partition by id),
value
Upvotes: 1