Author |
Topic |
kwikwisi
Constraint Violating Yak Guru
283 Posts |
Posted - 2012-01-28 : 12:03:26
|
Select col1, col2, createdon,... returns following records and I want to get only one record for each different col1 where createdon is the lastest(descending) among same col1 records. Can anyone help ? thanks.col1 col2 createdonabc 123 createdonabc 456 createdondef 789 createdondef 101 createdon |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2012-01-28 : 13:17:17
|
Look up Row_Number. Partition by Col1, order by createdon desc and filter for where that is 1--Gail ShawSQL Server MVP |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-28 : 17:47:30
|
another methodSELECT t1.*FROM table t1CROSS APPLY (SELECT MAX(createdon) AS latest FROM table WHERE col1= t1.col1 )t2WHERE t1.createdon = t2.latest ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kwikwisi
Constraint Violating Yak Guru
283 Posts |
Posted - 2012-01-29 : 22:13:01
|
Thanks.One thing is select statement is nested and getting syntax error when alias is set.Below is my query select ...from ..where .. in (select col from...where .. in (select ... ))Tcross apply(..)T2where .. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-29 : 22:15:35
|
cross apply should be before whereieselect ...from .. Tcross apply(..) T2where ..and .. in (select col from...where .. in (select ... )) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kwikwisi
Constraint Violating Yak Guru
283 Posts |
Posted - 2012-01-29 : 22:44:55
|
But select statement will complete with where clauses and it also contains join. Below is the query.select v.new_visitId, new_programvisitId,p.new_Name,v.new_admissiondatefrom new_visit v join new_program p on v.new_programvisitId = p.new_programId where v.new_programvisitId in ( select new_programId from new_program where new_referenceno in ( select name from Connection where RelatedConnectionId in ( select ConnectionId from Connection where Name = @param))) T CROSS APPLY (select MAX(new_admissiondate) AS latest from new_visit v join new_program p on v.new_programvisitId = p.new_programId where v.new_programvisitId in ( select new_programId from new_program where new_referenceno in ( select name from Connection where RelatedConnectionId in ( select ConnectionId from Connection where Name = @param))) )T2WHERE T.new_admissiondate = T2.latest |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-29 : 22:50:39
|
why include it twice? just once is enough right?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kwikwisi
Constraint Violating Yak Guru
283 Posts |
Posted - 2012-01-29 : 23:07:23
|
I didnt get you. First select will return all records and second select will return latest admission date. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-30 : 11:23:39
|
sorry i feel like you're complicating thingsshow us some sample data from table and what you need out of it and we will help you in getting it with much simpler query------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|