khsaunderson
Technical User
I am trying to insert a sequence into a table, but want to cycle the sequence dependent upon a specific field.
Using the above example I would want to take the Catalogue, which will appear multiple times, and increment the sequence for each instance of the Catalogue. Then when a new Catalogue is shown in the SELECT query, I would want the sequence to cycle and start back at 1.
Eg,
Catalogue Seq
Cat1 1
Cat1 2
Cat1 3
Cat2 1
Cat2 2
Cat3 1
Cat3 2
Cat3 3
Cat3 4
Any ideas on how I could code this?
Thanks
Code:
CREATE SEQUENCE TEMP_SEQ AS DECIMAL(5,0)
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CYCLE
CACHE 4;
INSERT INTO Table
(Catalogue char(27),
Sequence decimal(5,0))
SELECT
Catalogue,
NEXTVAL FOR TEMP_SEQ
FROM Catalogue
ORDER BY Catalogue
Using the above example I would want to take the Catalogue, which will appear multiple times, and increment the sequence for each instance of the Catalogue. Then when a new Catalogue is shown in the SELECT query, I would want the sequence to cycle and start back at 1.
Eg,
Catalogue Seq
Cat1 1
Cat1 2
Cat1 3
Cat2 1
Cat2 2
Cat3 1
Cat3 2
Cat3 3
Cat3 4
Any ideas on how I could code this?
Thanks