Home » SQL & PL/SQL » SQL & PL/SQL » Normalization (11.0.2)
Normalization [message #602939] Tue, 10 December 2013 08:43 Go to next message
pratik4891
Messages: 73
Registered: February 2011
Member
Below are the ddl and dml

   create table t1 
     (
     id1 varchar2(10),
     id2 varchar2(10),
     id3 varchar2(10),
     id4 varchar2(10)
     )



 insert into t1 values (1,null,null,null);
     insert into t1 values (null,'A',null,null);
     insert into t1 values (null,null,'Tom',null);
     insert into t1 values (null,null,null,32);
     insert into t1 values   (2,null,null,null);
     insert into t1 values (null,'B',null,null);
     insert into t1 values (null,null,'Henry',null);
     insert into t1 values (1,null,null,36);



I want the o/p as

ID1	ID2	ID3	ID4
1	A	Tom	32
2	B 	Henry 	36



The aggregation will be done on 4 rows only

Could anyone please help me out here ?
Thanks!!
Re: Normalization [message #602941 is a reply to message #602939] Tue, 10 December 2013 08:50 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>Could anyone please help me out here ?
I do not understand the problem or desired solution.
Re: Normalization [message #602944 is a reply to message #602939] Tue, 10 December 2013 08:52 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Why does id 1 associate with A, Tom and 32 and not B Tom and 36 or A Henry and 36 or some other combination?
Re: Normalization [message #602946 is a reply to message #602944] Tue, 10 December 2013 08:57 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
In case you don't realise - rows in a table are not stored in any order.
So if your output relies on knowing the order of insertion then you need a column to record the order of insertion.
As it stands there is no way to get your desired output.
Re: Normalization [message #602953 is a reply to message #602939] Tue, 10 December 2013 09:31 Go to previous messageGo to next message
pratik4891
Messages: 73
Registered: February 2011
Member
ok ...sorry for the confusion

I have a xml file it has 4 nodes ,for each iteration only one node contains data

Below is an example

<record>
<id1>1</id1>
<id2>null</id2>
<id3>null</id3>
<id4>null</id4>
</record>
<record>
<id1>null</id1>
<id2>A</id2>
<id3>null</id3>
<id4>null</id4>
</record>
<record>
<id1>null</id1>
<id2>null</id2>
<id3>Tom</id3>
<id4>null</id4>
</record>
<record>
<id1>null</id1>
<id2>null</id2>
<id3>null</id3>
<id4>32</id4>
</record>


the above 4 iterations makes one whole record

I can use a xml parser and make it as a row as I have mentioned previous post
Now I want to make a row out of it like

1 A Tom 32

If you have any suggestions please help me out

If there is none its fine also and I want to thank all of you on devoting your precious time to solve the problems
Re: Normalization [message #602969 is a reply to message #602953] Tue, 10 December 2013 10:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
SQL> col id1 format a10
SQL> col id2 format a10
SQL> col id3 format a10
SQL> col id4 format a10
SQL> with 
  2    data as (
  3      select xmltype('<records><record>
  4  <id1>1</id1>
  5  <id2>null</id2>
  6  <id3>null</id3>
  7  <id4>null</id4>
  8  </record>
  9  <record>
 10  <id1>null</id1>
 11  <id2>A</id2>
 12  <id3>null</id3>
 13  <id4>null</id4>
 14  </record>
 15  <record>
 16  <id1>null</id1>
 17  <id2>null</id2>
 18  <id3>Tom</id3>
 19  <id4>null</id4>
 20  </record>
 21  <record>
 22  <id1>null</id1>
 23  <id2>null</id2>
 24  <id3>null</id3>
 25  <id4>32</id4>
 26  </record>
 27  </records>') val from dual
 28    )
 29  select max(nullif(extractvalue(value(x), '//id1'),'null')) id1,
 30         max(nullif(extractvalue(value(x), '//id2'),'null')) id2,
 31         max(nullif(extractvalue(value(x), '//id3'),'null')) id3,
 32         max(nullif(extractvalue(value(x), '//id4'),'null')) id4
 33  from data,
 34       table(xmlsequence(extract(val, '//record'))) x
 35  /
ID1        ID2        ID3        ID4
---------- ---------- ---------- ----------
1          A          Tom        32
Re: Normalization [message #603103 is a reply to message #602969] Wed, 11 December 2013 07:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Feedback?

Re: Normalization [message #603126 is a reply to message #603103] Wed, 11 December 2013 09:14 Go to previous messageGo to next message
pratik4891
Messages: 73
Registered: February 2011
Member
Michel ,

Thanks a lot for your solution and time .

For my current project I can not use this code because there will be 10K records and I can not use with data option
Thats why I have wanted to parse the XML data to relational format and then normalize it

However its not possible that way , I got that

Thanks again for your solution and I have learnt about xml parsing in oracle
Re: Normalization [message #603127 is a reply to message #603126] Wed, 11 December 2013 09:15 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
You can lead some folks to knowledge, but you can not make them think.
Re: Normalization [message #603129 is a reply to message #603127] Wed, 11 December 2013 09:40 Go to previous messageGo to next message
pratik4891
Messages: 73
Registered: February 2011
Member
Actually I have figured this out in another way (via an ETL tool where row by row operation happens and you can aggregate the data with 4 records and taking the max value)

Thanks for all of your inputs
Re: Normalization [message #603130 is a reply to message #603126] Wed, 11 December 2013 09:56 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
I can not use with data option


This "data" option as you said is there ONLY because I didn't want to create a table containing your data.
Change it to:
SQL> insert into t values (xmltype('<records><record>
  2  <id1>1</id1>
  3  <id2>null</id2>
  4  <id3>null</id3>
  5  <id4>null</id4>
  6  </record>
  7  <record>
  8  <id1>null</id1>
  9  <id2>A</id2>
 10  <id3>null</id3>
 11  <id4>null</id4>
 12  </record>
 13  <record>
 14  <id1>null</id1>
 15  <id2>null</id2>
 16  <id3>Tom</id3>
 17  <id4>null</id4>
 18  </record>
 19  <record>
 20  <id1>null</id1>
 21  <id2>null</id2>
 22  <id3>null</id3>
 23  <id4>32</id4>
 24  </record>
 25  </records>')) ;

1 row created.

SQL> select max(nullif(extractvalue(value(x), '//id1'),'null')) id1,
  2         max(nullif(extractvalue(value(x), '//id2'),'null')) id2,
  3         max(nullif(extractvalue(value(x), '//id3'),'null')) id3,
  4         max(nullif(extractvalue(value(x), '//id4'),'null')) id4
  5  from t,
  6       table(xmlsequence(extract(val, '//record'))) x
  7  /
ID1        ID2        ID3        ID4
---------- ---------- ---------- ----------
1          A          Tom        32

1 row selected.

[Updated on: Wed, 11 December 2013 09:57]

Report message to a moderator

Re: Normalization [message #603134 is a reply to message #603130] Wed, 11 December 2013 10:12 Go to previous messageGo to next message
pratik4891
Messages: 73
Registered: February 2011
Member
Thanks Michel...will try that
Re: Normalization [message #603795 is a reply to message #603134] Thu, 19 December 2013 08:28 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
hi friend,

I have tried to find out solution for your problem.I tried my best and it's giving results as
you expected.I have done some hard coding also.Please apply your little bit logic for removing
hard coded values.

I hope my efforts can help you.

Please find below query as reference query :


select  (select max(id1) from t1 where ascii(substr(rowid,18,1)) between 65 and 68) as id1, a,b,c
from(
select  max(case when r >= 65 and r <= 68 then id1 end) as id1,
        max(id2) a,
        max(id3) b,
        max(nvl(id4,0)) c
from    
(
    select  ascii(substr(rowid,18,1)) as r,         
            id1,
            id2,
            id3,
            id4 
    from    t1
)
where r between 65 and 68
-----group by case when r >= 69 and r <= 72 then id1 end
)
where a is not null
union all
select  (select max(id1) from t1 where ascii(substr(rowid,18,1)) between 69 and 72) as id1, a,b,c
from(
select  max(case when r >= 69 and r <= 72 then id1 end) as id1,
        max(id2) a,
        max(id3) b,
        max(nvl(id4,0)) c
from    
(
    select  ascii(substr(rowid,18,1)) as r,         
            id1,
            id2,
            id3,
            id4 
    from    t1
)
where r between 69 and 72
-----group by case when r >= 69 and r <= 72 then id1 end
)
where a is not null

o/p :

ID1	A	B	C

1	A	Tom	32
2	B	Henry	36



[Updated on: Thu, 19 December 2013 08:29]

Report message to a moderator

Re: Normalization [message #603796 is a reply to message #603795] Thu, 19 December 2013 08:35 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
What on earth do you think this is doing:
ascii(substr(rowid,18,1))
Re: Normalization [message #603798 is a reply to message #603796] Thu, 19 December 2013 08:40 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Quote:


What on earth do you think this is doing:




Yes cookie,you are right,I have only given reference query.
Re: Normalization [message #603800 is a reply to message #603798] Thu, 19 December 2013 08:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

What does this mean "reference query"?

Re: Normalization [message #603801 is a reply to message #603800] Thu, 19 December 2013 08:49 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I have only given reference query.
how does a "reference query" differ from a non-reference query?
Re: Normalization [message #603802 is a reply to message #603800] Thu, 19 December 2013 08:49 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I have only given reference query.
how does a "reference query" differ from a non-reference query?
Re: Normalization [message #603803 is a reply to message #603800] Thu, 19 December 2013 08:50 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Quote:

What does this mean "reference query"?


hi Michel,

Reference means,if something can possible using this one then requester can use that.
Re: Normalization [message #603805 is a reply to message #603803] Thu, 19 December 2013 08:52 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Still don't understand what this mean.
You mean if it is correct then he can use it and if it is wrong then he can leave it?

Re: Normalization [message #603806 is a reply to message #603798] Thu, 19 December 2013 08:53 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So you've given a query that doesn't work?
Why?
Michel already gave the correct solution for the actual problem - which involves xml.
The problem from the original post is unsolvable as I already pointed out.
Re: Normalization [message #603807 is a reply to message #603806] Thu, 19 December 2013 08:54 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
No,

Query is giving result as requester is expecting.Please execute once if you have doubt
Re: Normalization [message #603811 is a reply to message #603807] Thu, 19 December 2013 09:13 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
It's only doing that because you got lucky with the rowids.
It will not work in general.
You can not rely on rowid to give data in the order of insertion, it's not designed to work that way.
Re: Normalization [message #603812 is a reply to message #603807] Thu, 19 December 2013 09:16 Go to previous message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So explain it.
I don't know if anyone understand what is the purpose of this "ascii(substr(rowid,18,1))"
And the rest neither.

In addition, it addresses a problem OP does not care and if you read the topic instead of posting, posting, posting without any thought you will know it, read http://www.orafaq.com/forum/mv/msg/190669/602953/#msg_602953
Quote:
ok ...sorry for the confusion

I have a xml file it has 4 nodes ,for each iteration only one node contains data

Below is an example

[Updated on: Thu, 04 February 2016 08:05]

Report message to a moderator

Previous Topic: Oracle Aggregation Problem in join between tables
Next Topic: Making a single query
Goto Forum:
  


Current Time: Fri Apr 26 19:56:48 CDT 2024