| Author |
Topic |
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2007-07-08 : 21:40:23
|
/*Hey guys, i need help to get record of TOP 3 in DESC order of DATE_CHARGED for each ACCOUNT NO and add RECORD_NO(autonumber) field in sequence of numbers starting from zero. Autonumber is also per ACCOUNT NO. I'm using SQL 2000The output below is the result that i wanted to produce. And please take a look at my query belowmaybe you could suggest additional queries so that i can derive the result.*/declare @sample table (account_no int, date_charged smalldatetime)insert into @sample select 1, '06/15/2006' union all select 1, '09/15/2006' union all select 1, '12/15/2006' union all select 1, '1/15/2007' union all select 1, '4/15/2007' union all select 1, '5/15/2007' union all select 1, '8/15/2007' union all select 2, '11/25/2006' union all select 2, '12/25/2006' union all select 2, '1/25/2007' union all select 2, '2/25/2007' union all select 2, '6/25/2007' union all select 2, '7/25/2007' union all select 2, '9/25/2007' union all select 4, '4/7/2006' union all select 4, '5/7/2006' union all select 4, '6/7/2006' union all select 4, '7/7/2006'/* --this is the expected outputaccount_no date_charged record_no1 2007-04-15 00:00:00 01 2007-05-15 00:00:00 11 2007-08-15 00:00:00 22 2007-06-25 00:00:00 02 2007-07-25 00:00:00 12 2007-09-25 00:00:00 24 2006-05-07 00:00:00 04 2006-06-07 00:00:00 14 2006-07-07 00:00:00 2I used a query to put a record number for each ACCOUNT_NO but the problem is it takes 12 seconds to put the autonumber field for 294,694 records. THIS QUERY IS VERY SLOW. :) This is the query that i used: select account_no, [date] as date_charged, (select count(*) from tblcharges where [date] <= c.[date] and account_no = c.account_no) from tblcharges corder by account_no,[date]tblCharges contains almost 300 thousand records so using the above query takes more than 12 seconds using pentium 4, 256 MB mem.*/ TCC |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-08 : 22:07:28
|
[code]SELECT *FROM( SELECT s.account_no, s.date_charged, rec_no = row_number() OVER (PARTITION BY account_no ORDER BY date_charged DESC) - 1 FROM @sample s) aWHERE rec_no <= 2[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2007-07-08 : 22:08:49
|
| An error has occuredServer: Msg 195, Level 15, State 10, Line 28'row_number' is not a recognized function name.TCC |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-08 : 22:09:34
|
are you using SQL Server 2005 or 2000 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2007-07-08 : 22:45:04
|
| I'm using SQL 2000, i forgot that you already have a different forum for SQL 2000TCC |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-08 : 22:56:05
|
{MOD : Please move to SQL 2000 Forum}As you have a large number of records in the table, it is best you do the rec_no in your front end. Any solution in SQL will not give you best performance. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
DaleJ
Starting Member
7 Posts |
Posted - 2007-07-09 : 04:24:06
|
| The first part of this will give you the groups of 3.The second assigns the record nbr. This part can be done in your UI or middle layer if needed, instead of the SS.select s.*into #tempfrom @sample sinner join( select distinct account_no from @sample) as liston s.account_no = list.account_noand s.date_charged in ( select top 3 t.date_charged from @sample t where t.account_no = s.account_no order by date_charged desc)order by s.account_no, s.date_chargedselect *, (select count(*) from #temp t2 where t2.account_no = t1.account_no and t2.date_charged < t1.date_charged) as record_nofrom #temp t1order by t1.account_no, t1.record_no |
 |
|
|
senpoly
Starting Member
19 Posts |
Posted - 2007-07-10 : 05:45:40
|
| if u are using sql 2005, this could be made easy by using cross apply |
 |
|
|
|