Solved

Union top 1

Posted on 2012-03-24
2
280 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
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 Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 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