Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
ORDER BY with priority

I want to use ORDER BY on uniqueid with priority. My table looks like this :

+-------------+------------------+
| disposition |     uniqueid     |  
+-------------+------------------+
| ANSWERED    | 1595409523.22546 |  
| NO ANSWER   | 1595409523.22546 |  
| BUSY        | 1595409523.22546 | 
| BUSY        | 1595406475.22391 |  
| NO ANSWER   | 1595406475.22391 |  
| BUSY        | 1595406475.22391 |  
+-------------+------------------+

I want for example on the ID: 1595409523.22546 to appear that the line where there is 'ANSWER', if there is no 'ANSWER' I want that 'NO ANSWER' is displayed otherwise BUSY etc ... The result should be :

+-------------+------------------+--+
| disposition |     uniqueid     |  |
+-------------+------------------+--+
| ANSWERED    | 1595409523.22546 |  |
| NO ANSWER   | 1595406475.22391 |  |
|             |                  |  |
+-------------+------------------+--+

Could you help me ? Thank you !

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

To check, for each uniqueid, a ranked list of dispositions and return the lowest ranked one found, or the lexically least disposition if none from the ranked list is found:

select
    if(min(find_in_set(disposition, 'ANSWERED,NO ANSWER,BUSY')),elt(min(find_in_set(disposition, 'ANSWERED,NO ANSWER,BUSY')),'ANSWERED,NO ANSWER,BUSY'),min(disposition)) disposition,
    uniqueid
from yourtable
group by uniqueid
over 4 years ago · Santiago Trujillo Report

0

If your MySql version is 8.0+ and supports window functions then you can use ROW_NUMBER():

select t.disposition, t.uniqueid
from (
  select *,
    row_number() over (partition by uniqueid 
                       order by field(disposition, 'ANSWERED', 'NO ANSWER', 'BUSY')) rn
  from tablename                                    
) t
where t.rn = 1

For earlier versions you can use conditional aggregation:

select case 1
         when max(disposition = 'ANSWERED') then 'ANSWERED'
         when max(disposition = 'NO ANSWER') then 'NO ANSWER'
         when max(disposition = 'BUSY') then 'BUSY' 
       end disposition,
       uniqueid  
from tablename
group by uniqueid

See the demo.
Results:

> disposition |         uniqueid
> :---------- | ---------------:
> ANSWERED    | 1595409523.22546
> NO ANSWER   | 1595406475.22391
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!