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 |
dean.c.cummins
Starting Member
6 Posts |
Posted - 2010-11-08 : 22:25:42
|
Im struggling to get my head around this one...i have the following sql query...select t_cpls,t_pric,t_stdtfrom ttdsls032100where t_item = '980100626000'order by t_cpls, t_stdt DESCwhich give the following dataset...001 5.3 2010-01-01 00:00:00.000001 5.9 2009-01-01 00:00:00.000001 6.2 2008-01-01 00:00:00.000002 7.43 2010-01-01 00:00:00.000002 7.43 2009-01-01 00:00:00.000002 7.43 2008-01-01 00:00:00.000003 6.5 2010-01-01 00:00:00.000003 6.5 2009-01-01 00:00:00.000003 5.49 2008-01-01 00:00:00.000005 5.93 2009-12-04 00:00:00.000005 5.75 2009-01-01 00:00:00.000006 6.12 2009-04-12 00:00:00.000006 5.94 2009-01-01 00:00:00.000007 6.34 2010-01-01 00:00:00.000008 5.25 2010-01-01 00:00:00.000ive highlighted the problems with this in bold. i basically want to only return the maximum date for a year per t_cpls! so it want it to produce the following dataset...001 5.3 2010-01-01 00:00:00.000001 5.9 2009-01-01 00:00:00.000001 6.2 2008-01-01 00:00:00.000002 7.43 2010-01-01 00:00:00.000002 7.43 2009-01-01 00:00:00.000002 7.43 2008-01-01 00:00:00.000003 6.5 2010-01-01 00:00:00.000003 6.5 2009-01-01 00:00:00.000003 5.49 2008-01-01 00:00:00.000005 5.93 2009-12-04 00:00:00.000006 6.12 2009-04-12 00:00:00.000007 6.34 2010-01-01 00:00:00.000008 5.25 2010-01-01 00:00:00.000any help would be greatly appreciated.many thanksDean x |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-11-09 : 01:41:38
|
[code]select t_cpls,t_pric,t_stdtfrom ttdsls032100 t inner join ( select t_cpls, t_stdt = max(t_stdt) from ttdsls032100 group by t_cpls, datepart(year, t_stdt) ) m on t.t_cpls = m.t_cpls and t.t_stdt = m.t_stdtwhere t_item = '980100626000'order by t_cpls, t_stdt DESC[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
dean.c.cummins
Starting Member
6 Posts |
Posted - 2010-11-09 : 06:01:47
|
many thanks :-) x |
|
|
|
|
|