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 |
|
getur.srikanth@gmail.com
Yak Posting Veteran
77 Posts |
Posted - 2009-10-12 : 11:53:33
|
My table looks like below from above table I need pick data like below I.e. '3391284' has two records but pick last (newly) ADD_DT value record (8/14/2008')'1691047' has 5 records and 4 has same last (newly) ADD_DT. here pick one record from 4 ADD_DTs. like 1691047 'Health,Safetym ....' 2/5/2008What SQL should I write to get this these records? |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-10-12 : 12:08:03
|
You don't mean "latest" - you mean "oldest". Am I right? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
getur.srikanth@gmail.com
Yak Posting Veteran
77 Posts |
Posted - 2009-10-12 : 12:09:40
|
| It mean, last ADD_DT date. Get from last ADD_DT data. |
 |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2009-10-12 : 15:09:47
|
| -- Setup test environmentDECLARE @MyTableTABLE([U##CONSTIT_ID] INT,CODE_DESC VARCHAR(50),ADD_DT DATETIME)INSERT @MyTableSELECT 3454445, 'Drilling and Completions', '9/26/09 2:33 AM'UNION ALLSELECT 3454444, 'Drilling and Completions', '9/26/09 2:29 AM'UNION ALLSELECT 3404825, 'Production and Operations', '9/26/09 12:15 AM'UNION ALLSELECT 3391284, 'Drilling and Completions', '8/14/08'UNION ALLSELECT 3391284, 'Production and Operations', '9/7/07 10:16 AM'UNION ALLSELECT 1691047, 'Health, Safety, Security, Environment', '2/5/08'UNION ALLSELECT 1691047, 'Production and Operations', '2/5/08'UNION ALLSELECT 1691047, 'Drilling and Completions', '2/5/08'UNION ALLSELECT 1691047, 'Projects, Facilities and Const', '2/5/08'UNION ALLSELECT 1691047, 'Reservoir Description and Dy', '2/5/08';-- Select Resultwith cte1AS(select *, ROW_NUMBER() OVER(PARTITION BY [U##CONSTIT_ID] ORDER BY ADD_DT DESC, CODE_DESC) AS SequenceFROM @MyTable)SELECT * FROM cte1 cWHERE c.Sequence = 1 |
 |
|
|
|
|
|
|
|