Home » SQL & PL/SQL » SQL & PL/SQL » how to give sequence number to my table group by id?
how to give sequence number to my table group by id? [message #21035] Mon, 08 July 2002 08:19 Go to next message
Jenny
Messages: 7
Registered: November 2001
Junior Member
I have a table "DEPT"(id, location, subid)
I want to insert some data such as:
1, abc, New York, 1
1, sfg, New York, 2
1, xyz, New York, 3
2, fff, San Hose, 1
2, ttt, San Hose, 2
I need to generate a sequence number for "subid" for the same id group. Can anybody help to give me some hints?
Thanks!

-jenny
Re: how to give sequence number to my table group by id? [message #21037 is a reply to message #21035] Mon, 08 July 2002 09:29 Go to previous messageGo to next message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
your example (and the name SUBID) suggest that this is a foreign key to a parent table. If so, then you cant just "generate" a sequence number for it. You need to identify the row in the parent table which you want to categorize that row in dept with and then take it's ID and use that value when you intert into dept.
Re: how to give sequence number to my table group by id? [message #21039 is a reply to message #21035] Mon, 08 July 2002 09:41 Go to previous messageGo to next message
Jenny
Messages: 7
Registered: November 2001
Junior Member
Andrew:
Unfortunately there is no parent table for this subid.
It is a new column just be added in. I am not sure how I can insert sequence to it. Can you show me the sql statement?
Thanks a lot.

-jenny
Re: how to give sequence number to my table group by id? [message #21078 is a reply to message #21035] Wed, 10 July 2002 19:34 Go to previous message
Su
Messages: 154
Registered: April 2002
Senior Member
Not sure what exactly you want. You are saying a subid and again you mean a sequence number for the same id group. I guess what you want is to give a sequence number that starts newly for new group, just like youhave mentioned in your example, 1,2,3 for New York, and 1,2 for San Jose.
If it is and I am right, you can use rank over().
Try this out.

SELECT ID, NAME, LOCATION, RANK() OVER(PARTITION BY LOCATION ORDER BY NAME) SUBID FROM your_table;

Please see your example lists 4 items in a row but your insert statement shows only three items. I assume it is by mistake and give example field name NAME for second item in the list.
Note: Since RANK OVER() is being used in a SELECT statement, INSERT can be given in conjuction with a SELECT to retrieve the date from another along with this ranking logic. Check it out.
Good luck :)
Previous Topic: Query to list the person who speaks every language !
Next Topic: rownum
Goto Forum:
  


Current Time: Thu Apr 25 20:28:11 CDT 2024