If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Informix > Row Seqeuence # Question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-07-09, 15:59
jhmfdavis jhmfdavis is offline
Registered User
 
Join Date: Aug 2009
Location: Poland, ME
Posts: 2
Unhappy Row Seqeuence # Question

Hi, I a newbie to the forum & informix.

I am using informix V10.x

I need to generate a sequence # based set of records and I am drawing a blank, a sample set of the records are:

Col1 Col2
2006 5
2006 5
2006 5
2006 6
2006 6

I need to generate with a select statement:

Col1 Col2 Seq
2006 5 1
2006 5 2
2006 5 3
2006 6 1
2006 6 2

Any ideas would be greatly appreciated and Thanks in advance for any help.
Reply With Quote
  #2 (permalink)  
Old 08-10-09, 16:33
skisalomon77 skisalomon77 is offline
Registered User
 
Join Date: May 2008
Posts: 21
Try something like this. I find it more reliable to create your own ROWIDs, rather than using the tables own ROWIDs.

This line creates an exact duplicate of your original table, except this table will contain a row_id column.

CREATE TABLE orig_tmp
(row_id CHAR(10), Col1 CHAR(4), Col2 CHAR(1));


This line creates the orig_tmp_1 table, which will include the minrow, col1 and col2 columns.

CREATE TEMP TABLE orig_tmp_1
(minrow CHAR(10), Col1 CHAR(4), Col2 CHAR(1)) WITH NO LOG;


This line selects the table's own rowid, col1 and col2 from your original table into orig_tmp.

INSERT INTO orig_tmp
SELECT rowid, col1, col2 FROM original_table;


This line is creating your sequence that increments by 1 and starts with 1.

CREATE SEQUENCE seq_2
INCREMENT BY 1 START WITH 1;


This line will update the row_id of the orig_tmp table.

UPDATE orig_tmp
SET row_id = seq_2.NEXTVAL;


This line will select the minimum rowid, col1 and col2 from the orig_tmp table and place that data into the orig_tmp_1 table and group the data by
col1 and col2.

INSERT INTO orig_tmp_1
SELECT MIN(row_id) minrow, Col1, Col2
FROM orig_tmp
GROUP BY col1, col2;


You'll need to join the tables from this point. I didn't include the script for the join. Anyway, you'll need to use something like this

(a.row_id-b.minrow))) as seq_num to obtain your unique values.

Last edited by skisalomon77; 08-10-09 at 16:50.
Reply With Quote
  #3 (permalink)  
Old 08-11-09, 11:08
jhmfdavis jhmfdavis is offline
Registered User
 
Join Date: Aug 2009
Location: Poland, ME
Posts: 2
Hi,

That worked great thanks. Just what I needed.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On