Solved

Union top 1

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
displaying 3 dropdownlist  horizontally 3 32
SQL Select Query help 1 38
aria difference 2 15
How do I Import CSV File In my PHP Application 29 21
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

730 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