[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

Order Union in SQL

Hello,
I am using a query with the UNION statement, something like this

SELECT 'Hello' as Suma
UNION
SELECT 'Saturday' as Suma
UNION
SELECT 'Friday' as Suma
UNION
SELECT 'Wednesday' as Suma


when I run I get :
Friday
Hello
Saturday
Wednesday

but I need to get

Hello
Saturday
Friday
Wednesday

some idea?
0
tenriquez199
Asked:
tenriquez199
  • 2
  • 2
1 Solution
 
Ephraim WangoyaCommented:
SELECT 'Hello' as Suma
UNION  ALL
SELECT 'Saturday' as Suma
UNION ALL
SELECT 'Friday' as Suma
UNION ALL
SELECT 'Wednesday' as Suma
0
 
dqmqCommented:
select suma from
(
SELECT 'Hello' as Suma, 1 as seq
UNION
SELECT 'Saturday', 2
UNION
SELECT 'Friday', 3
UNION
SELECT 'Wednesday', 4
) as x
order by seq asc

0
 
käµfm³d 👽Commented:
How about:
SELECT 'Hello', 1 as Suma
UNION 
SELECT 'Saturday', 2 as Suma
UNION 
SELECT 'Friday', 3 as Suma
UNION 
SELECT 'Wednesday', 4 as Suma
ORDER BY 2

Open in new window

0
 
käµfm³d 👽Commented:
Sorry dqmq...  I guess I'm too slow  = )
0
 
dqmqCommented:
Me too!  Except the accepted answer is only right by coincidence.   The only way to guarantee the ordering of rows returned by a select is with the ORDER BY CLAUSE.  I'm not arguing the the UNION ALL solution did not give the correct answer this time, just that it may not give the correct answer all the time.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now