Solved

Create a grouping column

Posted on 2012-03-19
7
257 Views
Last Modified: 2012-06-21
Create a Derived column
 Table Input
Level     Order
1            000001-01
2           000001.000001
2           000001.000002
1           000002-01
2           000002-01.000001-01
2          000002-01.000002-01
2          000002-01.000003-01

Desired Output
     Order                Grouping_Column (Derived Column)
     000001-01                       000001-01
     000001.000001                000001-01
     000001.000002                000001-01
     000002-01                       000002-01                
     000002-01.000001-01      000002-01
     000002-01.000002-01      000002-01
     000002-01.000003-01      000002-01
0
Comment
Question by:cookiejar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37740166
What are the rules?

I'm not seeing them from what you posted.  How do you get the derived column?

Also provide your Oracle version.
0
 
LVL 23

Expert Comment

by:David
ID: 37740168
What have you tried, or is this school work?
0
 

Author Comment

by:cookiejar
ID: 37740269
The derived column is what I would like to get from the input data.  

For example, the level  2s' rows should be  populated with 000001-01 in the derived column.
The 000002-01.000001-01 row derived column value should be   000002-01


1           000001-01
2           000001-01.000001-01
2           000001-01.000002-01
1           000002-01
2           000002-01.000001-01      

I just wanted to get an idea of how should I approach this. I'm  using ORACLE version 10.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37740289
I still do not understand the question.

How do you get from input to derived?

I do not see how 000002-01.000001-01 becomes 000002-01.  I can assume you just take everything from the left of the period bu then I don't get how you get from 000001.000002 to 000001-01.

>>'m  using ORACLE version 10.

10gR1 or 10gR2?
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37742287
I'll give you the benefit of the doubt that this is not school work but I agree it does sound a little like it.


Since you never posted back with the requirements, I'll guess at them.

All derived columns must end with a '-01'.  Take the left side if a decimal exists.  Add the '-01' if it does not naturally exist.

See if this gives ou what you need:

select derived || case when instr(derived,'-') = 0 then '-01' end
from (
select regexp_substr(col1,'[0-9-]+') derived from tab1
);
0
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 37743701
select order
,first_value(order) over (partition by substr(order,1,6) order by level) grouping_column
from input;

Note - level and order are keywords so column names have been modified in the example below -

SQL> select * from input;

      LEVL ORD
---------- --------------------
         1 000001-01
         2 000001.000001
         2 000001.000002
         1 000002-01
         2 000002-01.000001-01
         2 000002-01.000002-01
         2 000002-01.000003-01

SQL> select ord
  2  ,first_value(ord) over (partition b
g_column
  3  from input;

ORD                  GROUPING_COLUMN
-------------------- -------------------
000001-01            000001-01
000001.000001        000001-01
000001.000002        000001-01
000002-01            000002-01
000002-01.000001-01  000002-01
000002-01.000002-01  000002-01
000002-01.000003-01  000002-01
0
 
LVL 32

Expert Comment

by:awking00
ID: 37743710
Sorry, part of the query got cut off during cut and paste -
SQL> select ord
  2  ,first_value(ord) over (partition by substr(ord,1,6) order by levl) grouping_column
  3  from input;

ORD                  GROUPING_COLUMN
-------------------- --------------------
000001-01            000001-01
000001.000001        000001-01
000001.000002        000001-01
000002-01            000002-01
000002-01.000001-01  000002-01
000002-01.000002-01  000002-01
000002-01.000003-01  000002-01
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Suggested Courses

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question