Skip to content
Advertisement

How can I prioritize the maximum of multiple columns in SQLite

I have a little issue with my SQLite query which I use in python. In my little test_table I got 4 columns: sys_id, aq, vq, off_id. If I search for example for off_id=7477310

SELECT * FROM test_db WHERE off_id="7477310"

I get results:

sys_id  aq  vq  off_id
154803  50  40  7477310
153355  30  30  7477310
153455  50  40  7477310
163455  20  40  7477310

But I would also like a direct comparsion by this priority (vq,aq,sys_id) so I tought I use max. So the goal is if one result has the highest vq it directly wins, if vq ar identicial with more than one row next is aq, if aq is higher direct win, and last is sys_id if vq and aq is the same sys_id the highest sys_id wins.

Any ideas?

Thanks a lot

Advertisement

Answer

It looks like you want order by and limit:

select * 
from test_db 
where off_id = '7477310'
order by vq desc, aq desc, sys_id desc
limit 1
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement