Solved

join two resultsets in sql server

Posted on 2010-11-18
7
655 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 29

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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 29

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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