Mark C.
Mark C.

Reputation: 408

Ordering by two columns at the same time

I am creating a report for a call center. The extensions in the report can either be the source or the destination. I need the report to order by both src and dst simultaneously. So all calls that originated or terminated at the lowest number in the extension will list first, than the next number etc.. etc... The following query will give me all the calls in order for the src and then all the calls in order from the dst but I cannot figure out how to achieve my goal. Thanks for any help!

select src, dst, calldate, disposition from cdr where calldate > '2020-11-07' and calldate < '2020-11-12' 
and disposition = 'answered'
and ((src in ('2003', '2001', '2002', '2004', '2099', '2053', '2051')) 
or (dst in ('2003', '2001', '2002', '2004', '2099', '2053', '2051'))) order by src, dst;

Upvotes: 0

Views: 39

Answers (1)

GMB
GMB

Reputation: 222462

You seem to want:

order by least(src, dst), src, dst

This orders rows by the smallest value between src and dest; ties are broken by taking the smallest src, then the smallest dst.

Upvotes: 1

Related Questions