Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jchoudja
Starting Member
41 Posts |
Posted - 2013-04-02 : 10:59:18
|
Hi, Is it possible make a SELECT DISTINCT base on 2 Queries United by UNION ALLexampleSELECT ID, Name, DateIn, TimeIn FROM Table1UNION ALLSELECT ID, Name, DateIn, TimeIn From Table2ORDER BY ID, DateIn,TimeIn;I want a select a distinct value on ID from both queriesHow is this possible?Thank you.jc |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-04-02 : 11:55:17
|
if you're not concerned on which one you select, do likeSELECT ID,Name, DateIn,TimeInFROM(SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY TimeIn DESC) AS Seq,*FROM(SELECT ID, Name, DateIn, TimeIn FROM Table1UNION ALLSELECT ID, Name, DateIn, TimeIn From Table2)t)rWHERE Seq=1ORDER BY ID, DateIn,TimeIn; ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-04-02 : 12:52:57
|
Or perhaps you may need SELECT ID, Name, DateIn, TimeIn FROM Table1UNIONSELECT ID, Name, DateIn, TimeIn From Table2CheersMIK |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-04-02 : 13:03:25
|
quote: Originally posted by MIK_2008 Or perhaps you may need SELECT ID, Name, DateIn, TimeIn FROM Table1UNIONSELECT ID, Name, DateIn, TimeIn From Table2CheersMIK
it wont ensure uniqueness of ID values unless all other values are same------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-04-02 : 13:14:37
|
quote: Originally posted by visakh16
quote: Originally posted by MIK_2008 Or perhaps you may need SELECT ID, Name, DateIn, TimeIn FROM Table1UNIONSELECT ID, Name, DateIn, TimeIn From Table2CheersMIK
it wont ensure uniqueness of ID values unless all other values are same------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Correct :)CheersMIK |
|
|
|
|
|
|
|