473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

SQL UNION with Replace in the ORDER BY clause

I have a sql query that unions two recordsets. It works when I don't try to adjust the ORDER BY clause using the replace function. However, I want to ensure that a certain item in the list always floats to the top. Can someone help me?

Below is the SQL that does not run. Error is "Invalid column name 'name':

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.subcontractorID, subcontractor.[name]
from
subcontractor
order by
replace([name],'PRIME','AAA')


Also tried this but got error "ORDER BY items must appear in the select list if the statement contains a UNION operator.":

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.subcontractorID, subcontractor.[name]
from
subcontractor
order by
replace(2,'PRIME','AAA')



Below is the SQL that works:

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.subcontractorID, subcontractor.[name]
from
subcontractor
order by
[name]
Mar 5 '08 #1
4 9069
amitpatel66
2,367 Expert 2GB
I have a sql query that unions two recordsets. It works when I don't try to adjust the ORDER BY clause using the replace function. However, I want to ensure that a certain item in the list always floats to the top. Can someone help me?

Below is the SQL that does not run. Error is "Invalid column name 'name':

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.subcontractorID, subcontractor.[name]
from
subcontractor
order by
replace([name],'PRIME','AAA')


Also tried this but got error "ORDER BY items must appear in the select list if the statement contains a UNION operator.":

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.subcontractorID, subcontractor.[name]
from
subcontractor
order by
replace(2,'PRIME','AAA')



Below is the SQL that works:

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.subcontractorID, subcontractor.[name]
from
subcontractor
order by
[name]
Basically you are looking at 0 and PRIME to be at the TOP followed by the data is it??

Could you please provide some smaple data fetched by the second part of query in UNION?
Mar 5 '08 #2
ck9663
2,878 Expert 2GB
I have a sql query that unions two recordsets. It works when I don't try to adjust the ORDER BY clause using the replace function. However, I want to ensure that a certain item in the list always floats to the top. Can someone help me?

Below is the SQL that does not run. Error is "Invalid column name 'name':

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.subcontractorID, subcontractor.[name]
from
subcontractor
order by
replace([name],'PRIME','AAA')


Also tried this but got error "ORDER BY items must appear in the select list if the statement contains a UNION operator.":

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.subcontractorID, subcontractor.[name]
from
subcontractor
order by
replace(2,'PRIME','AAA')



Below is the SQL that works:

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.subcontractorID, subcontractor.[name]
from
subcontractor
order by
[name]
Sounds like you want this:

Expand|Select|Wrap|Line Numbers
  1. Select 0 as [subcontractorID], 'PRIME' as [name]
as your first record, right?

Try this:

Expand|Select|Wrap|Line Numbers
  1. Select 0 as [subcontractorID], 'PRIME' as [name], 0 as SortOrder
  2. UNION
  3. Select     
  4. subcontractor.subcontractorID, subcontractor.[name], 1 
  5. from subcontractor
  6. order by 3
  7.  
You can use order by 3, 1 to order it by SubContractorId after the SortOrder. If you don't want the SortOrder to show up, place the entire query in a subquery and remove the SortOrder on your SELECT list. You might need to include TOP keyword since you have ORDER BY inside the subquery.

Happy Coding.

-- CK
Mar 5 '08 #3
Thanks a lot - this worked. I used "ORDER BY 3, 2" The 2 allowed the multiple data records in the second select to be ordered by the subcontractor.name while still getting the 'PRIME' to stay at the top of the list.

Thanks again!



Sounds like you want this:

Expand|Select|Wrap|Line Numbers
  1. Select 0 as [subcontractorID], 'PRIME' as [name]
as your first record, right?

Try this:

Expand|Select|Wrap|Line Numbers
  1. Select 0 as [subcontractorID], 'PRIME' as [name], 0 as SortOrder
  2. UNION
  3. Select     
  4. subcontractor.subcontractorID, subcontractor.[name], 1 
  5. from subcontractor
  6. order by 3
  7.  
You can use order by 3, 1 to order it by SubContractorId after the SortOrder. If you don't want the SortOrder to show up, place the entire query in a subquery and remove the SortOrder on your SELECT list. You might need to include TOP keyword since you have ORDER BY inside the subquery.

Happy Coding.

-- CK
Mar 5 '08 #4
Hi

The reasons and ways to avoid this error have discussed in this

site with good examples. By making small changes in the query


http://sqlerror104.blogspot.com/2009...select_19.html
Aug 19 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: KENNY L. CHEN | last post by:
Dear experts, I have two tables in my Oracle 8i database: TEST (COL1,COl2,REC_NO) and TEST1 (COL1,COL2,REC_NO). Both tables are unique-indexed on (COL1,COL2,REC_NO). I think the following...
3
by: Blake Caraway | last post by:
All, I've seen several posts regarding using UNION or UNION ALL to mash together two or more resultsets into a single result set, but can't seem to find enough info here to help me answer my...
1
by: Tom Schindl | last post by:
Hi, the following Statement worked on MySQL 4.0 but after upgrading to 4.1.12 on win32 the order is not working any more. Is this a known problem or is our SQL simply not useable on 4.1 or is...
3
by: handaman | last post by:
I am having problems retrieving some data from a table, Tablename = "Magazine" Columns MagazineID MagazineName 1 2 Times 3 People 4 National...
4
by: robert | last post by:
before i go flapping my gums and get deeper in alligators, wondering whether the following makes sense. i have a database which provides picklists for input. it started out with a 2 column key...
4
by: shaun palmer | last post by:
when or Where do you use a union query ? how can I wright sql, tblPopulation,* tblcustomer,* one to one with all the appropriate attributes(field). Your help would be greatly...
2
by: S. van Beek | last post by:
Dear reader, The following code delivers a wild card in the result of the query. But the ORDER BY is not longer working.
1
by: piecko | last post by:
Hi everybody, I've got a problem with this SQL-Statement: (SELECT * FROM currencies WHERE name = 'EUR') UNION ALL (SELECT * FROM currencies WHERE name = 'USD') UNION ALL (SELECT * FROM...
3
by: dontfwithchuck | last post by:
Hi, this is my first post. I use Access XP. I have a single table called Movies containing the following fields: ID, Title, Genre, Rating, Location, Date I want a datasheet with the Titles...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.