INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Sequence

Sequence

(OP)
I am trying to insert a sequence into a table, but want to cycle the sequence dependent upon a specific field.  

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

RE: Sequence

CODE

INSERT CATALOGUE,SEQ INTO SOME_TABLE
SELECT
CATALOGUE,
ROW_NUMBER() OVER (PARTITION BY CATALOGUE ORDER BY CATALOGUE) as SEQ FROM CATALOGUE

Ties Blom
 
 

RE: Sequence

(OP)
Thanks.  For some reason I am getting an error though

CODE

SELECT
CATALOGUE,
ROW_NUMBER() OVER(PARTITION BY CATALOGUE ORDER BY CATALOGUE) AS SEQ
FROM HPINCFL

When I run this I get the following error:

SQL0104 - Token ( was not valid. Valid tokens: , FROM INTO.

Any idea why this might be?

Thanks

RE: Sequence

What's your platform? These functions don't work in every version of DB2, notably they don't generally work on a mainframe.

RE: Sequence

In terms of answering your problem, I would have thought that the best way to achieve this would be by using a trigger on the table.

Marc

RE: Sequence

(OP)
I'm using DB2 on an AS/400, so I guess that won't work then?

RE: Sequence

Nope, last time I checked AS400 did not support OLAP style functions. I think Marc is right about the trigger though..

Ties Blom
 
 

RE: Sequence

(OP)
Any help on how I would code the trigger?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close