Solved

How to write union and order in SQL?

Posted on 2009-05-04
13
478 Views
Last Modified: 2012-05-06
Hi

The following SQL has the error:
Incorrect syntax near the keyword 'union'.

If take out order by id, then it works.

However, it does not sort by id.


select currency from currency order by id union select '-- Please Select --' as currency from currency

Open in new window

0
Comment
Question by:techques
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
If you want to have Order by In your UNION Statement, then use order by in the last UNION query statement instead of the first query.
select currency from currency 

union select '-- Please Select --' as currency from currency

order by id 

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
select currency from (
select currency from currency, id
union
select '-- Please Select --' as currency,0 from currency) X
order by id
0
 

Author Comment

by:techques
Comment Utility
select currency from currency
union select '-- Please Select --' as currency from currency
order by id

it has error:
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.


select currency from (
select currency from currency, id
union
select '-- Please Select --' as currency,0 from currency) X
order by id

It has error:
Invalid object name 'id'.
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
id field must exists in currency table........... is it ?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
select      id,
            currency
from      currency

UNION ALL

select      0,
            '-- Please Select --'
from currency
order by id


0
 

Author Comment

by:techques
Comment Utility
Yes, there is id field in currency table.

select      id,
            currency
from      currency

UNION ALL

select      0,
            '-- Please Select --'
from currency
order by id


It can display, but it list out 29 rows of 0, -- Please Select -- duplicated records.

And, I do not need to display id=0 on the result.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Or if you do not want to see id in the selected list, use a derived table as in:

Select      currency
From      (
      select      id
                  currency
      from      currency

      UNION ALL

      select      0,
                  '-- Please Select --'
      from currency) d

order by id
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
You are right:

select      id,
            currency
from      currency

UNION ALL

select      0,
            '-- Please Select --'

order by id


Or:

Select      currency
From      (
      select      id
                  currency
      from      currency

      UNION ALL

      select      0,
                  '-- Please Select --'
       ) d

order by id
0
 

Author Comment

by:techques
Comment Utility
Select  currency
From      (
      select      id
                  currency
      from      currency

      UNION ALL

      select      0,
                  '-- Please Select --'
      from currency) d

order by id

It has error:
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 25 total points
Comment Utility
This should help you out:
SELECT currency

FROM (

select id,currency

from currency

UNION ALL

select 0,'-- Please Select --'

) temp

order by id

Open in new window

0
 

Author Comment

by:techques
Comment Utility
select      id,
            currency
from      currency

UNION ALL

select      0,
            '-- Please Select --'

order by id

It can display properly. But, how to take out the id from the result?
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 25 total points
Comment Utility
It is a typo, missing a comma.  It should be:
Select      currency
From      (
      select      id,
                        currency
      from            currency

      UNION ALL

      select      0,
                        '-- Please Select --'
       ) d

order by id
0
 

Author Closing Comment

by:techques
Comment Utility
both excellent!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now