I wand to select record from one table and insert into other . Here is my table 1
create table temp
(
seq varchar(20),
prefix varchar(20)
);
insert into temp values ('AAA','A'); // counter should be 1 as combo of AAA and A is 1
insert into temp values ('AAA','A'); // counter should be 2 as combo of AAA and A is 2
insert into temp values ('BBB','B'); // counter should be 1 as combo of BBB and B is 1
insert into temp values ('BBB','B'); // counter should be 2 as combo of BBB and B is 2
insert into temp values ('BBB','C'); // counter should be 1 as combo of BBB and C is 1
Now inserting from temp to temp_1
INSERT INTO temp_1 (seq,prefix,counter)
SELECT
seq, prefix,
(SELECT COUNT(*) FROM temp t
WHERE t.seq = t2.seq AND t.prefix = t2.prefix)
FROM temp t2;
2
Answers
You can make use of
row_number
for each group ofseq,prefix
example:
Most likely you need the window function row_number() for this. At least it gives you the results you’re looking for: