Author |
Topic |
cognos79
Posting Yak Master
241 Posts |
Posted - 2008-03-05 : 13:36:20
|
I do have following data. I need to get lastest record for each secid.Id SecId EFFDt SecName1 100 01/01/1990 Math1 100 01/01/1991 Math1 200 01/01/1990 Phys1 200 01/01/1991 PhysOutput should be:Id SecId EFFDt SecName1 100 01/01/1991 Math1 200 01/01/1991 PhysThanks... |
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-03-05 : 14:07:27
|
declare @a table ( Id int, SecId int, EFFDt datetime, SecName varchar(20))insert into @a select 1, 100, '01/01/1990', 'Math' union select 1, 100, '01/01/1991', 'Math' union select 1, 200, '01/01/1990', 'Phys' union select 1, 200, '01/01/1991', 'Phys' -- select * from @aselect id, SecID, max(EFFDt) as EFFDt, Max(SecName) as SecNamefrom @agroup by id, SecID"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
cognos79
Posting Yak Master
241 Posts |
Posted - 2008-03-05 : 14:23:02
|
slight modification...the secname can be modified when users update the record. so I need to gethold of the record that has the latest eff date...we cant use max(secname) at tht time...run the following query.declare @a table (Id int,SecId int,EFFDt datetime, SecName varchar(20))insert into @a select 1, 100, '01/01/1990', 'Mathasdfasdfasdf' union select 1, 100, '01/01/1991', 'Mat' union select 1, 100, '01/01/2001', 'M' union select 1, 100, '01/01/1991', 'Mat' union select 1, 200, '01/01/1990', 'Phys' union select 1, 200, '01/01/1991', 'Phys' select * from @aselect Id, SecId, max(EFFDt) as EFFDt, Max(SecName) as SecNamefrom @agroup by Id, SecIdthe resultset should contain this record1, 100, '01/01/2001', 'M' |
 |
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-03-05 : 14:35:26
|
[code]declare @a table ( Id int, SecId int, EFFDt datetime, SecName varchar(20))insert into @a select 1, 100, '01/01/1990', 'Mathasdfasdfasdf' union select 1, 100, '01/01/1991', 'Mat' union select 1, 100, '01/01/2001', 'M' union select 1, 100, '01/01/1991', 'Mat' union select 1, 200, '01/01/1990', 'Phys' union select 1, 200, '01/01/1991', 'Phys' select Id, SecId, max(EFFDt) as EFFDt, (select SecName from @a as X where ID = a.id and SecID = a.SECID and EFFDT = (select Max(EFFDT) from @a where ID = a.id and SecID = a.SECID )) as SecNamefrom @a as agroup by Id, SecId[/code]"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
|
|