Solved

Union top 1

Posted on 2012-03-24
2
294 Views
Last Modified: 2012-03-28
I am using union, like.

SELECT Id, StartDate From TableA
UNION
SELECT Id, StartDate From TableB

I would like to get top 1 ordered by StartDate. How is best to do this when I use Union?
0
Comment
Question by:johnkainn
[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 Comments
 
LVL 12

Assisted Solution

by:sachitjain
sachitjain earned 250 total points
ID: 37761934
Here it is

select top 1 Id, StartDate
from
      (SELECT Id, StartDate From TableA
      UNION
      SELECT Id, StartDate From TableB) L
order by StartDate
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 37766631
This is potentially much better from a performance standpoint:


SELECT TOP 1 Id, StartDate
FROM (
    SELECT TOP 1 Id, StartDate
    From TableA
    ORDER BY StartDate
    UNION
    SELECT TOP 1 Id, StartDate
    From TableB
    ORDER BY StartDate
) AS derived
ORDER BY StartDate
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

630 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