Solved

How to write union and order in SQL?

Posted on 2009-05-04
13
484 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24301603
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
ID: 24301606
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
ID: 24301611
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 39

Expert Comment

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

Expert Comment

by:Anthony Perkins
ID: 24301626
select      id,
            currency
from      currency

UNION ALL

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


0
 

Author Comment

by:techques
ID: 24301638
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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24301639
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
ID: 24301644
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
ID: 24301645
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
ID: 24301650
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
ID: 24301654
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
ID: 24301659
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
ID: 31577881
both excellent!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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