| Author |
Topic |
|
rajani
Constraint Violating Yak Guru
367 Posts |
Posted - 2006-06-20 : 21:45:29
|
| Hi friendsi have table with following fields(actually i've more flds)refdate --- datetimemasterid --- FK (int)referralid -- PK (int)what i want is ,i need to write sql stmt that gets latest record(i.e. refdate) for a master (i.e. masterid )if i have data like thismasterid,refdate ,referralid 4, 2002-10-22 12:34:20.000, 41225, 2002-12-20 10:16:26.000, 41234, 2002-12-20 10:24:44.000, 41244, 2002-12-20 10:47:29.000, 4125i want to see data like this5, 2002-12-20 10:16:26.000, 41234, 2002-12-20 10:47:29.000, 4125how can do that please ?Thanks for ur ideasCheers |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2006-06-20 : 21:52:21
|
| try to create the query using select with max and group by--------------------keeping it simple... |
 |
|
|
ramoneguru
Yak Posting Veteran
69 Posts |
Posted - 2006-06-20 : 21:54:00
|
| SELECT MAX(refdate) FROM <your table>WHERE masterID = <some masterID>That'll return the latest record....is that what you wanted?? *shrug*--Nick |
 |
|
|
rajani
Constraint Violating Yak Guru
367 Posts |
Posted - 2006-06-20 : 21:57:12
|
| actually latest record for each master (ie. FK).i sort of achieved what i want SELECT * FROM referralsINNER JOIN(SELECT max(refdate) AS refdate,max(ref.masterid) AS mastid FROM referrals AS ref GROUP BY ref.masterid) ref ON referrals.refdate = ref.refdate AND referrals.masterid = ref.mastidCheers |
 |
|
|
rajani
Constraint Violating Yak Guru
367 Posts |
Posted - 2006-06-20 : 21:58:49
|
| but my query may fail bcoz if there is same date happens to be there for same master.i mean if i have data like this4, 2002-12-20 10:24:44.000, 41244, 2002-12-20 10:24:44.000, 4125i want one record only ,in this case 4125 .how can i change my query ?Cheers |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-20 : 21:59:54
|
Or something like thisselect t.*from table tinner join ( select masterid, max(refdate) as max_refdate from table group by masterid) mon t.masterid = m.masteridand t.refdate = m.max_refdate KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-20 : 22:05:08
|
Just saw your last post. Is the referralid allocated in sequence ?Try thisselect t.*from table tinner join ( select masterid, max(refdate) as max_refdate, max(referralid) as max_referralid from table group by masterid) mon t.masterid = m.masteridand t.refdate = m.max_refdateand t.referralid = m.max_referralid KH |
 |
|
|
rajani
Constraint Violating Yak Guru
367 Posts |
Posted - 2006-06-20 : 22:07:25
|
| khi believe thats what my query is (see my post abv)but what if have same date for 2 records for same master?abv query will display two records ,wouldn't it ?Cheers |
 |
|
|
rajani
Constraint Violating Yak Guru
367 Posts |
Posted - 2006-06-20 : 22:13:21
|
| KHthats wonderful. thanksbtw, what happens if i have data like this4, 2005-12-22 10:24:44.000, 41244, 2002-12-20 10:24:44.000, 4125would it give me 1st record or 2nd ? actually 1st one thoughCheers |
 |
|
|
rajani
Constraint Violating Yak Guru
367 Posts |
Posted - 2006-06-20 : 22:14:16
|
| >> Is the referralid allocated in sequence ?yes it isCheers |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-20 : 22:50:52
|
Is the referralid unique ? KH |
 |
|
|
rajani
Constraint Violating Yak Guru
367 Posts |
Posted - 2006-06-20 : 22:54:04
|
| yup,it is.Cheers |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-20 : 22:56:24
|
[code]declare @table table( masterid int, refdate datetime, referralid int)insert into @tableselect 4, '2002-10-22 12:34:20.000', 4122 union allselect 5, '2002-12-20 10:16:26.000', 4123 union all -- *select 4, '2002-12-20 10:24:44.000', 4124 union allselect 4, '2002-12-20 10:47:29.000', 4125 union all -- *select 6, '2005-12-22 10:24:44.000', 4126 union all -- *select 6, '2002-12-20 10:24:44.000', 4127select t.*from @table tinner join( select masterid, max(referralid) as max_referralid from @table a where refdate = (select max(refdate) from @table x where x.masterid = a.masterid) group by masterid) mon t.masterid = m.masteridand t.referralid = m.max_referralid/* RESULTmasterid refdate referralid ----------- ------------------------------------------------------ ----------- 4 2002-12-20 10:47:29.000 41255 2002-12-20 10:16:26.000 41236 2005-12-22 10:24:44.000 4126*/[/code] KH |
 |
|
|
rajani
Constraint Violating Yak Guru
367 Posts |
Posted - 2006-06-20 : 23:10:18
|
| Thanks KH. that worked nicely :)Cheers |
 |
|
|
|