Solved

join two resultsets in sql server

Posted on 2010-11-18
7
650 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 28

Expert Comment

by:sammySeltzer
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I dediced to use my own solution...a temporary table vs using union.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now