473,770 Members | 4,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL UNION with Replace in the ORDER BY clause

2 New Member
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.s ubcontractorID, 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.s ubcontractorID, subcontractor.[name]
from
subcontractor
order by
replace(2,'PRIM E','AAA')



Below is the SQL that works:

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.s ubcontractorID, subcontractor.[name]
from
subcontractor
order by
[name]
Mar 5 '08 #1
4 9097
amitpatel66
2,367 Recognized Expert Top Contributor
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.s ubcontractorID, 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.s ubcontractorID, subcontractor.[name]
from
subcontractor
order by
replace(2,'PRIM E','AAA')



Below is the SQL that works:

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.s ubcontractorID, 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 Recognized Expert Specialist
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.s ubcontractorID, 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.s ubcontractorID, subcontractor.[name]
from
subcontractor
order by
replace(2,'PRIM E','AAA')



Below is the SQL that works:

Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.s ubcontractorID, 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
tkltechy
2 New Member
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.n ame 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
jobspider1000
4 New Member
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
12843
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 SQL commands will return the same result but one of my friends don't think so. He said "QUERY 1" will return 1 unsorted record (ROWNUM < 2 ) first then sort the result (ORDER BY COL1 ASC,
3
11976
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 particular question. I have a stored procedure that gets the column names in a particular format (i.e. "chassis_id"|"chassis_description"|"modify_date") as well as actual data for a given table (in a quote-separated, pipe-delimited
1
1676
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 there a bug in MySQL 4.1. ----------------------------8<---------------------------- ( select
3
15256
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 Geographic 5 US
4
3627
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 in the cross-reference table to the values table, call them company and state. it's a transitive closure. along the way, naturally, more columns have been added, call them region and division. so that now, the unique identifier from the...
4
2937
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 appreciated. Thank you.
2
4604
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
2051
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 currencies WHERE name !='EUR' AND name != 'USD' ORDER BY name ASC) The table "currencies" contains over 100 different currencies and I want get EUR first, then USD and at least the rest of the table in alphabetical order. But the problem seems to be...
3
4004
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 that have a date sorted in Descending order by date, then the Titles that don't have a date sorted by Title in Ascending order. I have this: SELECT & " " & "(" & & ")" AS Expr1
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.