sql server - Increment Row Number on Group for regular withdrawal amounts -
i need create sql server query groups "common" sequential data , assigns unique id group. in example below have withdrawals made policy on regular basis.
create table #temp2 (policy_id int, wdl_date int, amount decimal(9,2)); insert #temp2 values(1001, 19881028, 190.00); insert #temp2 values(1001, 19881129, 190.00); insert #temp2 values(1001, 19881229, 494.89); insert #temp2 values(1001, 19890130, 494.89); insert #temp2 values(1001, 19890227, 494.89); insert #temp2 values(1001, 19890330, 494.89); insert #temp2 values(1001, 19890530, 525.00); insert #temp2 values(1001, 19890629, 525.00); insert #temp2 values(1001, 19890728, 525.00); insert #temp2 values(1001, 19890830, 525.00); insert #temp2 values(1001, 19930723, 51.00); insert #temp2 values(1001, 19931213, 190.00); insert #temp2 values(1001, 19940311, 190.00); insert #temp2 values(1001, 19940613, 190.00); insert #temp2 values(1002, 19881028, 50.00); insert #temp2 values(1002, 19881129, 50.00); insert #temp2 values(1002, 19881229, 60.00); insert #temp2 values(1002, 19890130, 100.00); insert #temp2 values(1002, 19890227, 100.00);
i create output such below column id populated query. note amounts sorted in date order should determine values in field id.
policy_id wdl_date amount id --------- -------- ------ -- 1001 19881028 190.00 1 1001 19881129 190.00 1 1001 19881229 494.89 2 1001 19890130 494.89 2 1001 19890227 494.89 2 1001 19890330 494.89 2 1001 19890530 525.00 3 1001 19890629 525.00 3 1001 19890728 525.00 3 1001 19890830 525.00 3 1001 19930723 51.00 4 1001 19931213 190.00 5 1001 19940311 190.00 5 1001 19940613 190.00 5 1002 19881028 50.00 1 1002 19881129 50.00 1 1002 19881229 60.00 2 1002 19890130 100.00 3 1002 19890227 100.00 3
i've tried using dense_rank() populate id problem encounter 2 separate grouped amounts 190.00 (top 2 , bottom 3 records policy_id = 1001) need kept separate , dense_rank() statement considers both groups of 190.00 1 group. need grouping restart @ each new policy. code doesn't produce required output.
select *, dense_rank() on (order policy_id, amount) id #temp2 order policy_id, wdl_date;
can provide guidance on this?
to 190.00s need this...
select *, dense_rank() on (partition policy_id order amount) id #temp2 order policy_id, id;
your desired output has discrepancies when order amount in ranking function value 51.00 should first value not 190.00
Comments
Post a Comment