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 |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-22 : 08:26:03
|
| I have one lookup table and one main table in main table structure isID, Date, Statuslookup table structure isID, Datelookup table contains latest fail status entry for each idIn main table id may be muliple times but not in lookup I want to get distinct ID , Date, Status from main table with the latest date from lookup for that perticular IDbut its possible i may not have entry for some id in lookup tablebecause if status in success then entry will not go into lookup table even i want to get the data from main table and date also that might be success date or failure date.Vabhav T |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-22 : 08:35:06
|
| can you show some data and then explain? is it that you always want latest date record either from main or lookup?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-22 : 08:38:10
|
| Yes i want latest date record from either from main or lookup. but if it is in lookup table then it should show from look up only if its not then from main table.Vabhav T |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-22 : 08:41:32
|
quote: Originally posted by vaibhavktiwari83 Yes i want latest date record from either from main or lookup. but if it is in lookup table then it should show from look up only if its not then from main table.Vabhav T
SELECT ID,Date,StatusFROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(l.ID,t.ID) ORDER BY COALESCE(l.Date,t.Date) DESC) AS Seq,COALESCE(l.ID,t.ID) AS ID,COALESCE(l.Date,t.Date) AS DAte,t.StatusFROM main tLEFT JOIN lookup lON l.ID=t.ID)WHERE Seq=1 EDIT:corrected typo------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-02-22 : 08:44:09
|
quote: Originally posted by visakh16
quote: Originally posted by vaibhavktiwari83 Yes i want latest date record from either from main or lookup. but if it is in lookup table then it should show from look up only if its not then from main table.Vabhav T
SELECT ID,Date,StatusFROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(l.ID,t.ID) ORDER BY COALESCE(l.Date,t.Date) DESC) AS Seq,COALESCE(l.ID,t.ID) AS ID,COALESCE(l.Date,t.Date) AS DAte,t.StatusFROM main tLEFT JOIN lookup lON l.ID=t.ID)WHERE Seq=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Seems visakh made small typo error PBUH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-22 : 08:48:52
|
quote: Originally posted by Idera
quote: Originally posted by visakh16
quote: Originally posted by vaibhavktiwari83 Yes i want latest date record from either from main or lookup. but if it is in lookup table then it should show from look up only if its not then from main table.Vabhav T
SELECT ID,Date,StatusFROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(l.ID,t.ID) ORDER BY COALESCE(l.Date,t.Date) DESC) AS Seq,COALESCE(l.ID,t.ID) AS ID,COALESCE(l.Date,t.Date) AS DAte,t.StatusFROM main tLEFT JOIN lookup lON l.ID=t.ID)WHERE Seq=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Seems visakh made small typo error PBUH
just corrected it and refresh and saw that you've already spotted itNice catch ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-22 : 09:15:03
|
| again there is error of alias of inner query result set is missingSELECT ID,Date,StatusFROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(l.ID,t.ID) ORDER BY COALESCE(l.Date,t.Date) DESC) AS Seq,COALESCE(l.ID,t.ID) AS ID,COALESCE(l.Date,t.Date) AS DAte,t.StatusFROM main tLEFT JOIN lookup lON l.ID=t.ID) aWHERE Seq=1Vabhav T |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-22 : 09:21:33
|
| But thanks Visakh,Its really funtastic query it will help me a lot....Vabhav T |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-02-22 : 09:40:59
|
quote: Originally posted by visakh16
quote: Originally posted by vaibhavktiwari83 Yes i want latest date record from either from main or lookup. but if it is in lookup table then it should show from look up only if its not then from main table.Vabhav T
SELECT ID,Date,StatusFROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(l.ID,t.ID) ORDER BY COALESCE(l.Date,t.Date) DESC) AS Seq,COALESCE(l.ID,t.ID) AS ID,COALESCE(l.Date,t.Date) AS DAte,t.StatusFROM main tLEFT JOIN lookup lON l.ID=t.ID)WHERE Seq=1 EDIT:corrected typo------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Visakh you seems to have stopped using Apply clauses lately  SELECT ID,Date,StatusFROM(SELECT t.id,t.date,t.status,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS Seq from main t,OUTER APPLY(Select top 1 id from lookup l where l.id=t.id order by l.id)tbl)WHERE Seq=1 PBUH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-02-22 : 09:43:26
|
quote: Originally posted by Idera
quote: Originally posted by visakh16
quote: Originally posted by vaibhavktiwari83 Yes i want latest date record from either from main or lookup. but if it is in lookup table then it should show from look up only if its not then from main table.Vabhav T
SELECT ID,Date,StatusFROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(l.ID,t.ID) ORDER BY COALESCE(l.Date,t.Date) DESC) AS Seq,COALESCE(l.ID,t.ID) AS ID,COALESCE(l.Date,t.Date) AS DAte,t.StatusFROM main tLEFT JOIN lookup lON l.ID=t.ID)WHERE Seq=1 EDIT:corrected typo------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Visakh you seems to have stopped using Apply clauses lately  SELECT ID,Date,StatusFROM(SELECT t.id,t.date,t.status,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS Seq from main t,OUTER APPLY(Select top 1 id from lookup l where l.id=t.id order by l.id)tbl)WHERE Seq=1 PBUH
He is more specilised in CROSS-APPLY MadhivananFailing to plan is Planning to fail |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-02-22 : 10:34:48
|
quote: Originally posted by madhivanan
quote: Originally posted by Idera
quote: Originally posted by visakh16
quote: Originally posted by vaibhavktiwari83 Yes i want latest date record from either from main or lookup. but if it is in lookup table then it should show from look up only if its not then from main table.Vabhav T
SELECT ID,Date,StatusFROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(l.ID,t.ID) ORDER BY COALESCE(l.Date,t.Date) DESC) AS Seq,COALESCE(l.ID,t.ID) AS ID,COALESCE(l.Date,t.Date) AS DAte,t.StatusFROM main tLEFT JOIN lookup lON l.ID=t.ID)WHERE Seq=1 EDIT:corrected typo------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Visakh you seems to have stopped using Apply clauses lately  SELECT ID,Date,StatusFROM(SELECT t.id,t.date,t.status,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS Seq from main t,OUTER APPLY(Select top 1 id from lookup l where l.id=t.id order by l.id)tbl)WHERE Seq=1 PBUH
He is more specilised in CROSS-APPLY MadhivananFailing to plan is Planning to fail
Maybe that explains why.. PBUH |
 |
|
|
|
|
|
|
|