Solved

join two resultsets in sql server

Posted on 2010-11-18
7
653 Views
Last Modified: 2012-05-10
I have two resultsets I want to join.  But im not sure the easiest and best way.

Here are both queries:


SELECT     TOP (1) EventID,EventDescription,StartDate,EndDate,Highlight, Priority
FROM         vw_Events
WHERE     (Highlight = 'Yes' and Priority = 'Yes')
ORDER BY StartDate

SELECT     TOP (2) EventID,EventDescription,StartDate,EndDate,Highlight, Priority
FROM         vw_Events
WHERE     (Highlight = 'Yes' and Priority = 'NO')
ORDER BY StartDate
0
Comment
Question by:rochestermn
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 34167826
Try UNION:

SELECT     TOP (1) EventID,EventDescription,StartDate,EndDate,Highlight, Priority
FROM         vw_Events
WHERE     (Highlight = 'Yes' and Priority = 'Yes')
ORDER BY StartDate
UNION
SELECT     TOP (2) EventID,EventDescription,StartDate,EndDate,Highlight, Priority
FROM         vw_Events
WHERE     (Highlight = 'Yes' and Priority = 'NO')
ORDER BY StartDate 

Open in new window

0
 
LVL 41

Expert Comment

by:ralmada
ID: 34167837
what do you mean by "join". Do you mean merge? then use UNION ALL like below
SELECT     TOP (1) EventID,EventDescription,StartDate,EndDate,Highlight, Priority
FROM         vw_Events
WHERE     (Highlight = 'Yes' and Priority = 'Yes')
union all
SELECT     TOP (2) EventID,EventDescription,StartDate,EndDate,Highlight, Priority
FROM         vw_Events
WHERE     (Highlight = 'Yes' and Priority = 'NO')
ORDER BY StartDate

Open in new window

0
 
LVL 41

Expert Comment

by:ralmada
ID: 34167895
Actually try
select * from (
	SELECT     TOP 1 EventID,EventDescription,StartDate,EndDate,Highlight, Priority
	FROM         vw_Events
	WHERE     (Highlight = 'Yes' and Priority = 'Yes')
	order by StartDate
) a
union all
select * from (
	SELECT     TOP 2 EventID,EventDescription,StartDate,EndDate,Highlight, Priority
	FROM         vw_Events
	WHERE     (Highlight = 'Yes' and Priority = 'NO')
	ORDER BY StartDate
) b

Open in new window

0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 28

Expert Comment

by:sammySeltzer
ID: 34167929
Just in case you are wondering what the difference is between UNION and UNION ALL,

UNION ALL gives you everything including duplicates while UNION gives you an occurance of each record.

So, you choose based on your need but the solutions are there, I think.
0
 
LVL 2

Expert Comment

by:vasureddym
ID: 34188596
rochestermn, you can also try CTE to define (name) the two result set and do the union. something like below.



sammySeltzer: the difference between your code and ralmada's is not just the UNION vs UNION ALL. the ORDER BY clause cannot be directly used so. just an FYI.

with top1 as (
	SELECT     TOP (1) EventID,EventDescription,StartDate,EndDate,Highlight, Priority
	FROM         vw_Events
	WHERE     (Highlight = 'Yes' and Priority = 'Yes')
	ORDER BY StartDate
)
,top2 as (
	SELECT     TOP (2) EventID,EventDescription,StartDate,EndDate,Highlight, Priority
	FROM         vw_Events
	WHERE     (Highlight = 'Yes' and Priority = 'NO')
	ORDER BY StartDate
) 
select * from top1
union all
select * from top2;

Open in new window

0
 

Accepted Solution

by:
rochestermn earned 0 total points
ID: 34239610
I decided to go with a temp table.  It worked just fine.  Thanks for all the union suggestions though Im sure I will use them in the future.
0
 

Author Closing Comment

by:rochestermn
ID: 34276715
I dediced to use my own solution...a temporary table vs using union.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T-SQL - How to use like '%%' operator to search an integer 2 38
SQL Server 2012 r2 - Varible Table 3 32
Sql Server group by 10 44
TSQL recursive CTE challenge... 8 29
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question