473,406 Members | 2,894 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,406 software developers and data experts.

SQL Union with ORDER 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
6 Sports Illustrated
I am trying to bind a dropdownlist in .net with the 4 newest magazines
and the empty magazine.

SELECT TOP 1 MagazineID, MagazineName from Magazine
UNION
SELECT TOP 4 MagazineID, MagazineName from Magazine
Order by MagazineName;

The first part returns
1,''
The second part returns
1,''
2,'Times'
3,'People'
4,'National Geographic'

Given the above data, how can I get the sql results to display
5 'US'
4 'National Geographic'
3 'People'
2 'Times'
1 ''
Essentially i need to do the following

Select Top 1 MagazineID, MagazineName from Magazine Order by MagazineID

and add to it

Select Top 4 MagazineID, MagazineName from Magazine Order by MagazineID
desc

Jul 23 '05 #1
3 15240
Hi Handaman,

The query

SELECT TOP 1 MagazineID, MagazineName from Magazine

does not always return

1,''

Since you did not specify an ORDER BY clause, this statement could
return any 1 row of the table. If you want the empty magazine, you could
specify

SELECT MagazineID, MagazineName from Magazine WHERE MagazineName=''

The same holds true for your "SELECT TOP 4" statement. It will select 4
arbitrary rows from the table. What is your definition of "the 4 newest
magazines"? If a higher MagazineID means a newer magazine, then you
could write

SELECT MagazineID, MagazineName from Magazine WHERE MagazineName=''
UNION ALL
SELECT MagazineID, MagazineName FROM (
SELECT TOP 4 MagazineID, MagazineName
FROM Magazine
ORDER BY MagazineID DESC
) AS T1
ORDER BY MagazineName

Hope this helps,
Gert-Jan
handaman wrote:

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
6 Sports Illustrated

I am trying to bind a dropdownlist in .net with the 4 newest magazines
and the empty magazine.

SELECT TOP 1 MagazineID, MagazineName from Magazine
UNION
SELECT TOP 4 MagazineID, MagazineName from Magazine
Order by MagazineName;

The first part returns
1,''
The second part returns
1,''
2,'Times'
3,'People'
4,'National Geographic'

Given the above data, how can I get the sql results to display
5 'US'
4 'National Geographic'
3 'People'
2 'Times'
1 ''

Essentially i need to do the following

Select Top 1 MagazineID, MagazineName from Magazine Order by MagazineID

and add to it

Select Top 4 MagazineID, MagazineName from Magazine Order by MagazineID
desc

Jul 23 '05 #2
On 30 Mar 2005 11:29:19 -0800, handaman wrote:

(snip)
Essentially i need to do the following

Select Top 1 MagazineID, MagazineName from Magazine Order by MagazineID

and add to it

Select Top 4 MagazineID, MagazineName from Magazine Order by MagazineID
desc


Hi handaman,

In a UNION query, you can only have one ORDER BY clause, at the end, and
it will apply to the whole result, not to one of the UNION'ed SELECT
statements.

One possible workaround is to use derived tables, something like this:

SELECT ....
FROM (SELECT TOP 1 ...
FROM ...
ORDER BY ...) AS x
UNION ALL
SELECT ....
FROM (SELECT TOP 4 ...
FROM ...
ORDER BY ... DESC) AS y

However, in your case you could also make creative use of the CASE
expression in the ORDER BY clause:

SELECT TOP 5 MagazineID, MagazineName
FROM Magazine
ORDER BY CASE WHEN MagazineName = '' THEN 1 ELSE 2 END,
MagazineName DESC
(untested)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #3
select * from (Select Top 5 MagazineID, MagazineName from Magazine
Order by MagazineID) AS a order by MagazineID desc

Jul 23 '05 #4

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

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...
1
by: Adrian | last post by:
Dear All I've come across a strange issue when using UNION via MyODBC on my Fedora linux box (I'm using MySQL 4.1.3-standard-beta) **This doesn't work: (select a from tbl_name where a=10...
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...
5
by: NAJH | last post by:
I've been trying to do a union with a subquery - I've made a different example which follows the same principles as follows: First bit brings back accounts which are in the top 10 to 15 by...
1
by: Jeff Blee | last post by:
I hope someone can help me get this graph outputing in proper order. After help from Tom, I got a graph to display output from the previous 12 months and include the average of that output all in...
5
by: Alicia | last post by:
Hello everyone based on the data, I created a union query which produces this. SELECT ,,, 0 As ClosedCount FROM UNION SELECT ,, 0 AS OpenedCount, FROM ORDER BY , ;
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.
4
by: fluff | last post by:
Hello. I want to display some data from a database in a certain order. I have a table with columns A, B, Date. I want to display the records that have data in column A first and I want those...
1
by: kigoobe | last post by:
Hi friends, I'm having three queries that works perfectly ... SELECT ib.id as id, ib.titre as title, ib.date_expire as date_fin, ib.created_at as date_creation, eb.content as content, '' FROM...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.