473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Cursor: chronological order - guidance needed

2
Hello,
New to Oracle (9i), so hope some one can understand and help with this.

I want to add a episode number to a table, which will give the sequence of each episode. (Sorry for using the word sequence - not sure what else to call it).

Data looks like this to begin with:

ID Date Seq
aaa 1/1/07 <blank>
aaa 2/1/07 <blank>
bbb 1/1/07 <blank>
ccc 1/4/07 <blank>
ccc 11/4/07 <blank>

I am trying to get this:


ID Date Seq
aaa 1/1/07 1
aaa 2/1/07 2
bbb 1/1/07 1
ccc 1/4/07 1
ccc 11/4/07 2

ie for each id, the chronological order is placed in the sequence field.


I am trying to work with two cursors and an incrementing number, but getting nowhere fast. Is this the way to do it? Do I need to create a temporary table?


Please can someone offer some advice as to how I might progress this.(Please remember I am new to this!)

Thank you

R
Aug 21 '07 #1
2 1640
amitpatel66
2,367 Expert 2GB
Check out this anonymous block. Replace column names and table names as per yours:

Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2. int_cnt NUMBER := 1;
  3. h_date DATE;
  4. CURSOR C1 IS SELECT distinct id FROM temp_12;
  5. CURSOR C2(eid VARCHAR2) IS SELECT hdate from temp_12 where id = eid;
  6. BEGIN
  7. FOR I IN C1 LOOP
  8. IF(C1%FOUND) THEN
  9. OPEN C2(I.id);
  10. LOOP
  11. EXIT WHEN C2%NOTFOUND;
  12. FETCH C2 INTO h_date;
  13. UPDATE temp_12 SET seq = int_cnt WHERE id = I.id AND trunc(hdate) = trunc(h_date);
  14. COMMIT;
  15. int_cnt := int_cnt + 1;
  16. h_date := NULL;
  17. END LOOP;
  18. CLOSE C2;
  19. END IF;
  20. int_cnt:=1;
  21. END LOOP;
  22. END;
  23.  
Aug 22 '07 #2
amitpatel66
2,367 Expert 2GB
Simpler code to assign the sequence value:

Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2. CURSOR C1 IS select id,ROWID,hdate,seq,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY HDATE) rownumber FROM temp_12;
  3. BEGIN
  4. FOR I IN C1 LOOP
  5. UPDATE temp_12 SET SEQ = I.rownumber WHERE id = I.id AND TRUNC(hdate) = TRUNC(I.hdate) AND ROWID = I.ROWID;
  6. COMMIT;
  7. END LOOP;
  8. END;
  9.  
I am using ROWID here to avoid problems in case when ID and hdate values are the same.
Aug 23 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: php newbie | last post by:
I am trying to write a cursor to update certain rows in a particular order as follows: (I need the cursor version, not SQL, as the update logic depends on the order of rows and some other...
15
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I...
1
by: jfro | last post by:
After trying every way I could come up with I can't get a filter clause to work with a passed variable ... I have a cursor that pulls a filter string from a table (works OK), then I want to use...
10
by: Neil | last post by:
I need to get two values from a complex SQL statement which returns a single record and use those two values to update a single record in a table. In order to assign those two values to variables...
0
by: Alexander Vlasenko | last post by:
Please CC me, I am not subscribed. An imaginary SQL statement INSERT INTO table FETCH ... FROM cursor; looks almost the same as currently available INSERT INTO table SELECT ...; I tried it...
1
by: traceable1 | last post by:
SQL Server 2000 SP4 with AWE hotfix. Windows 2003 SP1. I have a stored procedure which is not working the way I think it should be. I have a CURSOR which has a variable in the WHERE clause: ...
0
by: anonieko | last post by:
This approach I found very efficient and FAST when compared to the rowcount, or Subquery Approaches. This is before the advent of a ranking function from DB such as ROW_NUMBER() in SQL Server...
0
by: mh1hep | last post by:
I have two DB2 cursors accessing the same tabel but in adifferent order. I open the first one, fetch all the data, Then close it. When I open the second one with the new order, it starts at the...
1
by: yeohyc | last post by:
Hi All, I have a problem here. I have done a request for move order it was stored into a custom table with the format: (O)336126(G)83(I)21823(M)630065120(L)LOT-DISB2P-01(Q)2500...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.