Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Union top 1

Posted on 2012-03-24
2
Medium Priority
?
303 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 1000 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 70

Accepted Solution

by:
Scott Pletcher earned 1000 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
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…
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…

715 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