Solved

Union top 1

Posted on 2012-03-24
2
276 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:
ScottPletcher 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

762 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

20 Experts available now in Live!

Get 1:1 Help Now