noushad
noushad

Reputation: 175

Use order by in union all

I have a query

select username,amount from employee
union all
select '' as username,sum(amount) from employee
order by cast(username as decimal)

username start from 1000

when I use this query it always display highest username to smallest username

I want to display smallest username to highest username

What I do for that?

Upvotes: 0

Views: 83

Answers (1)

John Woo
John Woo

Reputation: 263933

try it by wrapping in in a subquery,

SELECT *
FROM
    (
        SELECT username, amount from employee
        UNION ALL
        SELECT '' as username, sum(amount) from employee
    ) x
ORDER BY (CASE WHEN username = '' THEN 1 ELSE 0 END) ASC, 
          CAST(username AS SIGNED) ASC

Upvotes: 1

Related Questions