jeudi 31 décembre 2015

SQlite update incremental counter

I have the following table:
Group | SubGroup | id
'A' | 'AA' | 2
'A' | 'AB' | 4
'A' | 'AC' | 6
'B' | 'BA' | 2
'B' | 'BB' | 9

And I would like to update the id column to have sequentially value for each group:
Group | SubGroup | id
'A' | 'AA' | 1
'A' | 'AB' | 2
'A' | 'AC' | 3
'B' | 'BA' | 1
'B' | 'BB' | 2

I used a query to ask the value and got it but don't know how to make the update.

CREATE TABLE temp_tbl (groupa TEXT, subgroup TEXT, num INTEGER);  
INSERT INTO temp_tbl(groupa,subgroup,num) VALUES ('A','AA',2);   
INSERT INTO temp_tbl(groupa,subgroup,num) VALUES ('A','AB',4);  
INSERT INTO temp_tbl(groupa,subgroup,num) VALUES ('A','AC',6);  
INSERT INTO temp_tbl(groupa,subgroup,num) VALUES ('B','BA',2);  
INSERT INTO temp_tbl(groupa,subgroup,num) VALUES ('B','BB',9); 

'A' | 'AA' | 1
'A' | 'AB' | 2
'A' | 'AC' | 3
'B' | 'BA' | 1
'B' | 'BB' | 2
Now I would like to update the field accordingly. How could I do it?

Thanks

Aucun commentaire:

Enregistrer un commentaire