Home > Software engineering >  How to apply row_number for each partition with condition
How to apply row_number for each partition with condition

Time:12-17

I want to add a column named PLRowNo which is row number of PL actype in each voucher group. I also want the numbering to start from 1 for every voucher number.


This is the expected result

| id | voucherID | actype | PLRowNo |
|----|-----------| -------|---------|
| 1  | voucher01 | BS     |         |
| 2  | voucher01 | PL     | 1       |
| 3  | voucher01 | BS     |         |
| 4  | voucher01 | PL     | 2       |
| 5  | voucher01 | PL     | 3       |
| 6  | voucher01 | BS     |         |       
| 7  | voucher01 | PL     | 4       |
| 8  | voucher01 | BS     |         |
| 9  | voucher01 | PL     | 5       |
| 10 | voucher02 | PL     |         |
| 11 | voucher02 | PL     | 1       |
| 12 | voucher02 | BS     |         |
| 13 | voucher02 | PL     | 2       |

This is what I have tried:

CREATE TABLE tbl_tmp (
    id int not null primary key,
    voucherID nvarchar(10) not null,
    actype nvarchar(10) not null
);

insert into tbl_tmp(id,voucherID, actype)
values (1,'voucher01', 'BS'),
        (2,'voucher01', 'PL'),
        (3,'voucher01', 'BS'),
        (4,'voucher01', 'PL'),
        (5,'voucher01', 'PL'),
        (6,'voucher01', 'BS'),
        (7,'voucher01', 'PL'),
        (8,'voucher01', 'BS'),
        (9,'voucher01', 'PL'),
        (10,'voucher02', 'PL'),
        (11,'voucher02', 'PL'),
        (12,'voucher02', 'BS'),
        (13,'voucher02', 'PL')

select *,0 as PLRowNo into #tmp from tbl_tmp
declare @id int set @id=0

update #tmp
set @id= case when actype ='PL' then @id 1   else 0 end,
 PLRowNo = case when actype='PL' then @id else 0 end

select * from #tmp

The problem with this query is, it starts counting from 1 (id: 13) if the previous row's type is 'BS'even though in the same voucher partition. I want the continuation within one voucher.


This is the wrong result

| id | voucherID | actype | PLRowNo |
|----|-----------| -------|---------|
| 1  | voucher01 | BS     |         |
| 2  | voucher01 | PL     | 1       |
| 3  | voucher01 | BS     |         |
| 4  | voucher01 | PL     | 2       |
| 5  | voucher01 | PL     | 3       |
| 6  | voucher01 | BS     |         |       
| 7  | voucher01 | PL     | 4       |
| 8  | voucher01 | BS     |         |
| 9  | voucher01 | PL     | 5       |
| 10 | voucher02 | PL     |         |
| 11 | voucher02 | PL     | 1       |
| 12 | voucher02 | BS     |         |
| 13 | voucher02 | PL     | 1       |

CodePudding user response:

You don't need temp tables or quirky updates, you can just use ROW_NUMBER

SELECT *,
  PLRow_no = CASE WHEN actype = 'PL' THEN
    ROW_NUMBER() OVER (PARTITION BY actype, voucherID ORDER BY id) 
    END
FROM tbl_tmp
ORDER BY id;

db<>fiddle

CodePudding user response:

Please try this.

select *,    
CASE
    WHEN actype='PL' THEN CAST((select count(*) from tbl_tmp as t where actype='PL' and t.id<=tbl_tmp.id and  t.voucherID=tbl_tmp.voucherID )as VarChar(10))
    ELSE ''
END
as PLRowNo     
from tbl_tmp
  • Related