473,379 Members | 1,544 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,379 software developers and data experts.

Order By in Subquery + UNION

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_dat e") as well
as actual data for a given table (in a quote-separated, pipe-delimited
manner i.e. "1"|"description for the chassis"|"2004-09-08").

I'd like to get both of these resultsets and mash them together. This
works, but when I need to order the second resultset (i.e. select *
from chassis order by chassis_id), SQL Server returns an error
complaining about the chassis_id column name (invalid column name
'chassis_id') in the Order By clause.

From what I can tell, I'm using the UNION and Order By in correctly,
but I'm not sure exactly what's wrong with it. If I take out the Order
By, everything works great. Although I would like to be able to order
my second resultset (in the same sproc) if possible.

The actual queries I'm running are actually quite long, but here's one
that's a bit shorter to help illustrate:

SELECT '"app_group_id"|"app_group_name"|"create_date"|"cr eate_by"|"modify_date"|"modify_by"'
UNION ALL
SELECT + ISNULL(CONVERT(varchar,app_group_id), '') + '|'
+ SUBSTRING(RTRIM(LTRIM(CONVERT(varchar, 1))), 1,0)
+ '"' + ISNULL(CONVERT(varchar(1000), +
REPLACE(CONVERT(nvarchar(4000),app_group_name), '"', '""')), '') +
'"|'
+ SUBSTRING(RTRIM(LTRIM(CONVERT(varchar, 2))), 1,0)
FROM app_group
order by app_group_id
Thank for any help on this.

/bc
Jul 20 '05 #1
3 11950
In a UNION any columns in the ORDER BY list must appear in the result. If
you don't want to include the ORDER BY column in the result then nest the
UNION as a derived table:

SELECT x
FROM
(SELECT NULL /* app_group_id column */ ,
... etc
UNION ALL
SELECT app_group_id,
... etc
FROM app_group) AS T(app_group_id,x)
ORDER BY app_group_id

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
CREATE A VIEW WITHOUT THE ORDER BY CLAUSE. THEN SELECT COLUMNS FROM
VIEW USING THE ORDER BY CLAUSE. IF I REMEMBER RIGHT, UNION DOES NOT
LIKE ORDER BY.

Thanks, Girish
bl***@caraways.net (Blake Caraway) wrote in message news:<90**************************@posting.google. com>...
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_dat e") as well
as actual data for a given table (in a quote-separated, pipe-delimited
manner i.e. "1"|"description for the chassis"|"2004-09-08").

I'd like to get both of these resultsets and mash them together. This
works, but when I need to order the second resultset (i.e. select *
from chassis order by chassis_id), SQL Server returns an error
complaining about the chassis_id column name (invalid column name
'chassis_id') in the Order By clause.

From what I can tell, I'm using the UNION and Order By in correctly,
but I'm not sure exactly what's wrong with it. If I take out the Order
By, everything works great. Although I would like to be able to order
my second resultset (in the same sproc) if possible.

The actual queries I'm running are actually quite long, but here's one
that's a bit shorter to help illustrate:

SELECT '"app_group_id"|"app_group_name"|"create_date"|"cr eate_by"|"modify_date"|"modify_by"'
UNION ALL
SELECT + ISNULL(CONVERT(varchar,app_group_id), '') + '|'
+ SUBSTRING(RTRIM(LTRIM(CONVERT(varchar, 1))), 1,0)
+ '"' + ISNULL(CONVERT(varchar(1000), +
REPLACE(CONVERT(nvarchar(4000),app_group_name), '"', '""')), '') +
'"|'
+ SUBSTRING(RTRIM(LTRIM(CONVERT(varchar, 2))), 1,0)
FROM app_group
order by app_group_id
Thank for any help on this.

/bc

Jul 20 '05 #3
Girish (ka********@hotmail.com) writes:
CREATE A VIEW WITHOUT THE ORDER BY CLAUSE. THEN SELECT COLUMNS FROM
VIEW USING THE ORDER BY CLAUSE. IF I REMEMBER RIGHT, UNION DOES NOT
LIKE ORDER BY.


You remember wrong. But it is imporant to understand that ORDER BY
applies to the entire query, not the individual SELECT clauses in the
query.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Evgeny Gopengauz | last post by:
Let us suppose that we have a table: CREATE TABLE transactions( currencyID_1 int, value_1 money, currencyID_2 int, value_2 money )
10
by: sqlgoogle | last post by:
Hi I'm trying to update a db based on the select statement which has ORDER BY in it. And due to that I'm getting error which states that Server: Msg 1033, Level 15, State 1, Line 13 The ORDER...
4
by: pgp.coppens | last post by:
All, Seeing the behaviour below on DB2 v8 on zOS create table test(intcol integer); insert into test values (1); insert into test values (2); insert into test values (3); insert into test...
2
by: edself | last post by:
Greetings, I am semi-new to Access and have a query question. I presume the solution is easy, but need some help. I have created a database with a Contact table. The contact table contains...
2
by: elein | last post by:
Yes, I vacuumed. Reproduced on both 7.3.2 and 7.5. Brain dead java beans want order by clauses in views that they use. my view is: select .... from bigtable b left join lookuptable l order...
22
by: Kevin Murphy | last post by:
I'm using PG 7.4.3 on Mac OS X. I am disappointed with the performance of queries like 'select foo from bar where baz in (subquery)', or updates like 'update bar set foo = 2 where baz in...
7
by: Arnold | last post by:
Greetings Gurus, In a mainform's header, I have a combobox named comboStudents. The rowsource for this combobox is: SELECT -999 As StudentID, "<Add New Student>" As FullName, "aaa" As...
3
by: laurentc via AccessMonster.com | last post by:
Hi. I have an issue with my Access project. I have rather big tables of data (about 11 000 rows). These tables are historical product quotations, so they are very simple : - MyDate...
3
by: Yobbo | last post by:
Hi All My query is as follows: SELECT STRINGTEXT, TOKENID FROM WEBSTRINGS WHERE TOKENID IN (6,20,234,19,32,4,800,177) All I want is my resultset to come back in the order that I have...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.