| Author |
Topic |
|
yogaanand
Starting Member
3 Posts |
Posted - 2009-02-21 : 00:34:07
|
| hi friendsi have one table with date column. i have one record or more record .. i want to take the second record date ..i wrote the query but its taking time to run..exTable name is 'yoga'ID DateTime58381 2008-08-12 15:49:33.56058381 2008-08-12 16:03:03.74758381 2008-08-12 18:13:17.747i wrote query like thisselect top 1 datetime from yoga where datetime in(select top 2 datetime from yoga where id=58381order by datetime)order by datetime descif i run for one id means , it's coming fast but i want add this query ii reports , so its taking timecan any one solution please ..ThanksYogaanandyogi |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-21 : 00:43:06
|
| try any one of thesedeclare @tab table (ID int, DateTime datetime)insert into @tab select 58381, '2008-08-12 15:49:33.560' union all select 58381, '2008-08-12 16:03:03.747' union all select 58381, '2008-08-12 18:13:17.747'select top 1 * from @tab where DateTime < (select max(DateTime) from @tab) order by datetime descselect id,datetime from (select row_number() over(partition by id order by id) as rid , * from @tab)t where rid = 2 |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-21 : 00:44:01
|
| select * from ( select *,row_number() over ( partition by ID order by id ) rn from table ) twhere t.rn = 2 |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-21 : 01:01:23
|
| select id,max(datetime) from @temp where datetime <( select max(datetime) from @temp)group by id |
 |
|
|
yogaanand
Starting Member
3 Posts |
Posted - 2009-02-21 : 01:30:47
|
| thanksyogi |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-21 : 01:32:18
|
quote: Originally posted by yogaanand thanksyogi
welcome |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-21 : 01:43:16
|
quote: Originally posted by yogaanand thanksyogi
Welcome |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-02-21 : 01:43:45
|
| http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/find-nth-maximum-value.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-21 : 07:34:10
|
If you are looking for 2nd max date.Select ID,[DateTime]from(Select Dense_Rank() Over(Partition by ID Order by [Datetime]desc)as ROWID,* from yourtable)ZWhere Z.ROWID =2 |
 |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-28 : 16:30:42
|
| I have a master table with 1,946 ids and want to add date and value to their respective columns, How can I use this select to update the columns?Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc)as ROWID,* from obs where hdid = 28)ZWhere Z.ROWID =1and pid in (select pt_pid1 from bds_diabetic_values_tmg)group by pid,obsvalue,obsdatethanks |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-04-28 : 16:48:47
|
Something like thisUpdate yourtable set yourtable.obsdate=T.obsdate inner join(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc)as ROWID,* from obs where hdid = 28)ZWhere Z.ROWID =1and pid in (select pt_pid1 from bds_diabetic_values_tmg)group by pid,obsvalue,obsdate)T on T.pid=yourtable.pid PBUH |
 |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-29 : 08:36:03
|
| Hi sodeepyour example works but I found some exceptions. Sometimes patients have the test on the same day so there are 2 results on same day. so if I do Where Z.ROWID =1 to get the first test I get 2 rows for some ids because they had a test on 9/5/09 for 6.6. and 9/5/09 for 6.3. How to edit code to get both from 9/5/09 to list as most recent and the second one on same day as 2nd most recent using Where Z.ROWID =2thanks! |
 |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-29 : 08:45:29
|
| I guess I could use a cursor but I try to avoid them |
 |
|
|
|