rochestermn
asked on
join two resultsets in sql server
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,S tartDate,E ndDate,Hig hlight, Priority
FROM vw_Events
WHERE (Highlight = 'Yes' and Priority = 'Yes')
ORDER BY StartDate
SELECT TOP (2) EventID,EventDescription,S tartDate,E ndDate,Hig hlight, Priority
FROM vw_Events
WHERE (Highlight = 'Yes' and Priority = 'NO')
ORDER BY StartDate
Here are both queries:
SELECT TOP (1) EventID,EventDescription,S
FROM vw_Events
WHERE (Highlight = 'Yes' and Priority = 'Yes')
ORDER BY StartDate
SELECT TOP (2) EventID,EventDescription,S
FROM vw_Events
WHERE (Highlight = 'Yes' and Priority = 'NO')
ORDER BY StartDate
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
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
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.
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.
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.
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I dediced to use my own solution...a temporary table vs using union.
Open in new window