473,544 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'joining' results of 2 queries

Does anyone know how I can 'join' the results of
one SQL query to the bottom of another?

Eg. I have two queries:
1. SELECT Name, Surname FROM People WHERE Surname = Smith
NAME SURNAME

Adam Smith
John Smith
Michael Smith
Steve Smith
2. SELECT Name, Surname FROM People WHERE Surname = Jones
NAME SURNAME

Bob Jones
Larry Jones
Tom Jones

What I want to produce is:
NAME SURNAME

Adam Smith
John Smith
Michael Smith
Steve Smith
Bob Jones
Larry Jones
Tom Jones
However, if I use UNION like this:

SELECT Name, Surname FROM People WHERE Surname = Smith
UNION
SELECT Name, Surname FROM People WHERE Surname = Jones
it mixes up all the results:

NAME SURNAME

Adam Smith
Bob Jones
John Smith
Larry Jones
Michael Smith
Steve Smith
Tom Jones
(I guess it's sorting by the first field, NAME).

Is there a way to stop it sorting the results, so that it
just tacks the second query results to the bottom of the
first query results?
(I realise I could use "ORDER BY Surname" to get the same result
in this simple example, but for the more complicated queries
I want to use it won't work).

Thanks for any help,

Matt.
Jul 20 '05 #1
3 12877
Hi Matt,

Union sorts the data and tries to remove duplicates. Use UNION ALL to
override this behavior. If the data is stored in a table, use an
identity column to preserve the order. - Louis
Jul 20 '05 #2
od************* *@hotmail.com (Matt O'Donnell) wrote in message news:<cc******* *************** ****@posting.go ogle.com>...
Does anyone know how I can 'join' the results of
one SQL query to the bottom of another?

Eg. I have two queries:
1. SELECT Name, Surname FROM People WHERE Surname = Smith
NAME SURNAME

Adam Smith
John Smith
Michael Smith
Steve Smith
2. SELECT Name, Surname FROM People WHERE Surname = Jones
NAME SURNAME

Bob Jones
Larry Jones
Tom Jones

What I want to produce is:
NAME SURNAME

Adam Smith
John Smith
Michael Smith
Steve Smith
Bob Jones
Larry Jones
Tom Jones
However, if I use UNION like this:

SELECT Name, Surname FROM People WHERE Surname = Smith
UNION
SELECT Name, Surname FROM People WHERE Surname = Jones
it mixes up all the results:

NAME SURNAME

Adam Smith
Bob Jones
John Smith
Larry Jones
Michael Smith
Steve Smith
Tom Jones
(I guess it's sorting by the first field, NAME).

Is there a way to stop it sorting the results, so that it
just tacks the second query results to the bottom of the
first query results?
(I realise I could use "ORDER BY Surname" to get the same result
in this simple example, but for the more complicated queries
I want to use it won't work).

Thanks for any help,

Matt.

WHy not use

SELECT Name, Surname FROM People WHERE Surname = 'Jones' or 'SMITH'
ORDER BY Surname, Name
Simple and avoids the need for a UNION

HOpe this helps
Duncan
Jul 20 '05 #3
I agree that in this case you should just use an OR, but assuming your real
case is more complicated, how about

SELECT 1 AS ResultSetNum, Name, Surname FROM People WHERE Surname = Smith
UNION ALL
SELECT 2 AS ResultSetNum, Name, Surname FROM People WHERE Surname = Jones
ORDER BY ResultSetNum

"duncan" <dw***@woodace. co.uk> wrote in message
news:8f******** *************** ***@posting.goo gle.com...
od************* *@hotmail.com (Matt O'Donnell) wrote in message

news:<cc******* *************** ****@posting.go ogle.com>...
Does anyone know how I can 'join' the results of
one SQL query to the bottom of another?

Eg. I have two queries:
1. SELECT Name, Surname FROM People WHERE Surname = Smith
NAME SURNAME

Adam Smith
John Smith
Michael Smith
Steve Smith
2. SELECT Name, Surname FROM People WHERE Surname = Jones
NAME SURNAME

Bob Jones
Larry Jones
Tom Jones

What I want to produce is:
NAME SURNAME

Adam Smith
John Smith
Michael Smith
Steve Smith
Bob Jones
Larry Jones
Tom Jones
However, if I use UNION like this:

SELECT Name, Surname FROM People WHERE Surname = Smith
UNION
SELECT Name, Surname FROM People WHERE Surname = Jones
it mixes up all the results:

NAME SURNAME

Adam Smith
Bob Jones
John Smith
Larry Jones
Michael Smith
Steve Smith
Tom Jones
(I guess it's sorting by the first field, NAME).

Is there a way to stop it sorting the results, so that it
just tacks the second query results to the bottom of the
first query results?
(I realise I could use "ORDER BY Surname" to get the same result
in this simple example, but for the more complicated queries
I want to use it won't work).

Thanks for any help,

Matt.

WHy not use

SELECT Name, Surname FROM People WHERE Surname = 'Jones' or 'SMITH'
ORDER BY Surname, Name
Simple and avoids the need for a UNION

HOpe this helps
Duncan

Jul 20 '05 #4

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

Similar topics

6
1701
by: paulus4605 | last post by:
Dears I have the following problem I’m using a query to get all the data from my database from the past year the second query is displaying the results by month. How can I match the second query with the first query in order to get the results from the same store on the same row?
1
1563
by: Ondernemer | last post by:
I have a table with 2 records: ========== TABLE: users ========== name_id (INT) name (varchar) code (int)
1
2261
by: Beachvolleyballer | last post by:
hi there anyone had an idea to join following 2 queries to 1???? ----- QUERY 1 --------------------------------------------- SELECT TMS_CaseF_2.Name AS TCDomain_0, TMS_CaseF_3.Name AS TCDomain_1, TMS.CaseF.Name AS TCFolder_2, TMS_CaseF_1.Name AS TCFolder_3,
0
1264
by: Norma | last post by:
I have 2 queries that pull attendance records depending on the type of attendance occurance, or Both queries are drawn off 1 table where the field will depict whether it is a punctuality or attendance occurance. I would like to create a report that will total each type of absence in its own column. Can anyone help me with this?
14
2539
by: Martin Lacoste | last post by:
Access 2000 Trying even the most basic queries - select queries joining (inner join) two tables - and the results are not updatable, and I can't tell why they aren't. All permissions are on, recordset is dynaset, unique values is no, etc.. etc.. Here's the SQL if it helps..: SELECT tbl.ID FROM tbl INNER JOIN Master ON tbl.IDNumber =...
2
1664
by: Alec | last post by:
Newbie question. Its really an SQL question, but have had no reply from the SQL newsgroup. I want to search the same table for two different criteria, and then join the search results together into one new list. See below. <?php
4
2611
by: dsdevonsomer | last post by:
Hello, I have one simple query joining two tables with left outer join on 3 fields and using MIN on two fields. These two tables have lot of data about 3 mil in total. I am trying to migrate db from MS Access to SQL 2005. I get about 689000 rows in SQL Server, vs 863000 rows in MS Access. SELECT T1., T1., T2., MIN ( T1.), MIN(T1. ),...
0
1381
eWish
by: eWish | last post by:
I am not sure that I am taking the proper approach or even have the best design for what I am doing. I have a table that I am storing attributes the for a real estate property listing. Each property could have one or several (many to many) attributes. Then I am storing the non-redundant data in the listing table. Instead of using two...
0
1729
by: iceman23 | last post by:
Hello, SCENARIO: I would like to have these 2 queries be combined and the results as well. I tried to add QUERY2 in the WHERE clause of QUERY1, what I got was multiple field instances as a result. Included below the the query/result that I would wnt to achieve. Thanks in advance! QUERY 1
0
7370
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7617
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7387
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7714
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...
0
5920
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...
1
5305
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...
0
3415
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1848
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
0
674
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...

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.