This is probably really simple, but I’ve been trying to figure this out for a while, maybe a fresh pair of eyes will/could help.
http://sqlfiddle.com/#!9/557e5a/1
I have this table wherein I need to prioritize selecting Accounts that are Chequing first, and if they don’t have a Chequing account, then select the Savings account.
As you can see, PartyID C only has a Savings account, and everyone else has either both, or only a Chequing account.
I tried SELECT PartyID, MIN(Description), MIN(Number) FROM Table GROUP By PartyID
and that gave me what I needed for the most part, but for PartyID E, it’s returning the Business Savings account instead of their Chequing account.
The desired output result would be this
Any help would be great.
2
Answers
Use a window function to rank the rows within each party ID, ranking
Chequing
beforeSavings
.DEMO
Using analytic function
row_number
(note that analytic functions are supported from MySQL version 8.0 onwards):