Question

SQL numbering based on a pattern

Asked by: ewang1205

I need to populate the Numbering column based on two other columns pattern.  If my node level =2 and appears the second time then number = 1200.  Difficult to explain, please see the follwing data.  

Node_Level    Tree_seq           Numbering
1                       1                       1000  
2                        2                       1100
3                        3                     1110
4                        4                     1111
2                        5                     1200
3                        6                      1210
2                        7                     1300
3                        8                      1310

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-06 at 12:20:03ID24879169
Topics

Oracle 9.x

,

Oracle Database

Participating Experts
3
Points
500
Comments
28

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Pattern search and change
    I have an audit table that holds old and new values. I want to be able to search the audit table for values that are only numeric in the old and new values. The old and new value fields are character fields and are populated with any changes made in any table. Is there a way ...
  2. Pattern Matching
    I need to match inconsistant data in the sql server database. when people are filling in their company names in a registration form, they all put in different names for the same company. eg: Intel Intel Corp Intel Corporation Intel Corporation. ...... so ma...
  3. LIKE search for multiple patterns?
    OK SQL gurus -- can I use "LIKE" to search for multiple patterns? Let's say I want to select rows with a fieldr that starts with either "CK1%" or "CK2" - isn't there a way to do something like: select * from cr_repository where field like ('...
  4. Meaning of  "(.*)\"  in a pattern.
    What do "(.*)\" do in the pattern below? elsif ( $line =~ m/^\|\s+AT\_name\s+\"(.*)\"/ ) { }

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: sdstuberPosted on 2009-11-06 at 12:43:46ID: 25762867

you need starting values for each numbering for the first occurence of a node_level.

I assume you already have those in your numbering column and you just need to populate the 2nd and subsequent occurences of each node_level


SELECT node_level,
       tree_seq,
       CASE WHEN rn = 0 THEN numbering ELSE first_numbering + (100 * rn) END numbering
FROM (SELECT node_level,
             tree_seq,
             numbering,
             ROW_NUMBER() OVER (PARTITION BY node_level ORDER BY tree_seq) - 1 rn,
             FIRST_VALUE(numbering) OVER (PARTITION BY node_level ORDER BY tree_seq) first_numbering
      FROM yourtable)
ORDER BY tree_seq

 

by: sdstuberPosted on 2009-11-06 at 12:44:16ID: 25762870

if you don't have starting values in a numbering column already, how do you get the sequences started?


 

by: ewang1205Posted on 2009-11-06 at 12:49:57ID: 25762912

The numbering column is blank and I need a select statement to get numbering based on two other columns.

 

by: sdstuberPosted on 2009-11-06 at 12:58:12ID: 25762989

yes, but you have only explained what you want to happen for the 2nd occurrence of a node_level.

Looks like you want to add 100 each time a node_level repeats
you haven't explained how you are getting your starting values though.

why is node_level 1  1000  and node_level 2  1100  the first time?

 

by: ewang1205Posted on 2009-11-06 at 13:02:52ID: 25763031

Node_leve 1 default to 1000.  
Then the first time node_level =2 the value will be 1100...  
Then the first time node_level =3 the value will be 1X10   X is the numbering for previous node_level =2.  
Then the first time node_level =4 the value will be 1XY1   X is the numbering for previous node_level =2.  

Thanks.

 

by: sdstuberPosted on 2009-11-06 at 13:09:27ID: 25763081

So, effectively 1,2,3,4 are hardcoded for starting points

Since 1 and 2 are fixed and 3 and 4 derived from them then they aren't really dynamic either

 

by: slightwvPosted on 2009-11-06 at 13:10:21ID: 25763085

Where does tree_seq com into play?

If I think I understand isn't your original sample data wrong?

Shouldn't
2                        5                     1200
be
2                        5                     1210

and
3                        6                      1210
be
3                        6                      1220

 

by: dqmqPosted on 2009-11-06 at 13:11:09ID: 25763090

Try this:


Select

(Select cast(count(*) as char(1)) from YOURTABLE T2
   where T2.Tree_Seq <= T1.Tree_Seq
       and T2.node_level = T1.node_level
       and T1.node_level = 1)
||
(Select cast(count(*) as char(1)) from YOURTABLE T2
   where T2.Tree_Seq <= T1.Tree_Seq
       and T2.node_level = T1.node_level
       and T1.node_level = 2)
(Select cast(count(*) as char(1)) from YOURTABLE T2
   where T2.Tree_Seq <= T1.Tree_Seq
       and T2.node_level = T1.node_level
       and T1.node_level = 3)
||
(Select cast(count(*) as char(1)) from YOURTABLE T2
   where T2.Tree_Seq <= T1.Tree_Seq
       and T2.node_level = T1.node_level
       and T1.node_level = 4)
From YOURTABLE T1


 

by: dqmqPosted on 2009-11-06 at 13:12:12ID: 25763097

Try again:
Select
(Select cast(count(*) as char(1)) from YOURTABLE T2
   where T2.Tree_Seq <= T1.Tree_Seq
       and T2.node_level = T1.node_level
       and T1.node_level = 1)
||
(Select cast(count(*) as char(1)) from YOURTABLE T2
   where T2.Tree_Seq <= T1.Tree_Seq
       and T2.node_level = T1.node_level
       and T1.node_level = 2)
||
(Select cast(count(*) as char(1)) from YOURTABLE T2
   where T2.Tree_Seq <= T1.Tree_Seq
       and T2.node_level = T1.node_level
       and T1.node_level = 3)
||
(Select cast(count(*) as char(1)) from YOURTABLE T2
   where T2.Tree_Seq <= T1.Tree_Seq
       and T2.node_level = T1.node_level
       and T1.node_level = 4)
From YOURTABLE T1

 

by: ewang1205Posted on 2009-11-06 at 13:13:48ID: 25763110

Sample data is correct.  2                        5                     1200   because level =2 will always have 1X00.

 

by: sdstuberPosted on 2009-11-06 at 13:15:18ID: 25763119

since the first numberings are fixed you can embed them at the lowest level instead of deriving them

SELECT node_level,
       tree_seq,
       first_numbering + (100 * rn)numbering
FROM (SELECT node_level,
             tree_seq,
             CASE
                 WHEN node_level = 1 THEN 1000
                 WHEN node_level = 2 THEN 1100
                 WHEN node_level = 3 THEN 1110
                 WHEN node_level = 4 THEN 1111
                 ELSE 0
             END
                 first_numbering,
             ROW_NUMBER() OVER (PARTITION BY node_level ORDER BY tree_seq) - 1 rn
      FROM yourtable)
ORDER BY tree_seq

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:

Select allOpen in new window

 

by: ewang1205Posted on 2009-11-06 at 13:17:17ID: 25763132

dqmq:  That is not the pattern.  if node_level =3 then the pattern will be 1XX0.  


SET DEFINE OFF;
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (4, 4);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (2, 5);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (3, 6);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (1, 1);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (2, 2);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (3, 3);
COMMIT;

 

by: ewang1205Posted on 2009-11-06 at 13:19:26ID: 25763152

sdstuber:  Your solution looks good so far.  Let me test more data...

 

by: sdstuberPosted on 2009-11-06 at 13:21:24ID: 25763171

I'm not sure what you're trying to say by 1X10 and 1XY1

your starting points appear to be constants

 

by: sdstuberPosted on 2009-11-06 at 13:22:13ID: 25763175

ok, I must have guessed your intent correctly then.  :)


 

by: ewang1205Posted on 2009-11-06 at 13:22:42ID: 25763182

X can be 1, 2, 3,....  Yes, we are talking about numbers.  

 

by: ewang1205Posted on 2009-11-06 at 13:51:59ID: 25763394

sdstuber:   Here is data.  Node_level =4 tree_seq should be 1311 but your result is 1211.  


SET DEFINE OFF;
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (4, 4);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (2, 5);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (3, 6);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (2, 7);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (3, 8);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (4, 9);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (1, 1);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (2, 2);
Insert into T
   (NODE_LEVEL, TREE_SEQ)
 Values
   (3, 3);
COMMIT;

 

by: sdstuberPosted on 2009-11-06 at 13:59:59ID: 25763458

why would the second level 4 be 1311?

there is only one prior level 4 before it

please explain how you want your data to increment

 

by: ewang1205Posted on 2009-11-06 at 14:04:28ID: 25763494

1311 because its level 2 (seq =7) is 1300 and its level 3 (seq =8)  is 1310.  

 

by: sdstuberPosted on 2009-11-06 at 14:06:22ID: 25763500

I don't understand

please explain the entire set of sequencing rules.

how do seq7 and seq8  relate to it?

 

by: ewang1205Posted on 2009-11-06 at 14:11:34ID: 25763525

The whole data set is like a parent child relationship, if you look at this way.  Implied,  seq7 is actually the parent of seq8 and seq8 is the parent of seq9.  My initial post on spec is not complete.  

 

by: ewang1205Posted on 2009-11-06 at 14:14:07ID: 25763547

We look another way, if seq7 =2 and number =1300 then seq8 =3 and number will be 1310 ...  
Now if seq9  = 4 then numbering will be 1311, if seq=2 then numbering will be 1400.  

 

by: slightwvPosted on 2009-11-06 at 20:10:55ID: 25764953

I'm with sdstuber.  I don't understand.

I still don't see where tab_seq comes into play.

I thought it was node occurrence = number offset.  Where nodes occurence=numbers ABCD  

so
node 1 = 1000
node 2 = 0100
node 3 = 0010
node 4 = 0001

 adn every node occurrence increments the offeset so second node 3 would be 0020.

Please set us straight.

 

by: ewang1205Posted on 2009-11-07 at 08:32:51ID: 25766886

Please see column description.  Then you will see the logic behind numbering.  


LEVEL      DESCRIPTION      TREE_SEQ NUMBERING
                  
1      WORLD            1      1000
2      USA            2      1100
3      TEXAS            3      1110
4      HOUSTON            4      1111
2      INDIA            5      1200
3      INDIAN STATE1      6      1210
2      CHINA            7      1300
3      CHINA PROVINCE1      8      1310
4       PROVINCE1 CITY      9      1311

 

by: sdstuberPosted on 2009-11-07 at 10:10:59ID: 25767323

ok, thanks for the last example.

the problem is you must derive the parent/child relationships.  they data itself doesn't define it directly.

    SELECT   node_level,
             tree_seq,
             RPAD(REPLACE(SYS_CONNECT_BY_PATH(r, '/'), '/', NULL), 4, '0') numbering
      FROM   (SELECT   node_level,
                       tree_seq,
                       parent_node,
                       ROW_NUMBER() OVER (PARTITION BY parent_node ORDER BY tree_seq) r
                FROM   (SELECT   t1.*,
                                 (SELECT   MAX(tree_seq)
                                    FROM   t t2
                                   WHERE   t2.node_level = t1.node_level - 1
                                       AND t2.tree_seq < t1.tree_seq)
                                     parent_node
                          FROM   t t1))
CONNECT BY   parent_node = PRIOR tree_seq
START WITH   parent_node IS NULL
  ORDER BY   tree_seq

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:

Select allOpen in new window

 

by: ewang1205Posted on 2009-11-07 at 12:26:18ID: 25767880

sdstuber:  Looks good.  I am going to test with more data.  

 

by: ewang1205Posted on 2009-11-09 at 07:41:41ID: 31651209

Thanks for the help!

 

by: sdstuberPosted on 2009-11-09 at 07:50:27ID: 25776933

glad I could help!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...