Normalization [message #602939] |
Tue, 10 December 2013 08:43 |
|
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 #602944 is a reply to message #602939] |
Tue, 10 December 2013 08:52 |
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 |
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 #602969 is a reply to message #602953] |
Tue, 10 December 2013 10:29 |
|
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 #603130 is a reply to message #603126] |
Wed, 11 December 2013 09:56 |
|
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 #603795 is a reply to message #603134] |
Thu, 19 December 2013 08:28 |
|
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 #603806 is a reply to message #603798] |
Thu, 19 December 2013 08:53 |
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 #603811 is a reply to message #603807] |
Thu, 19 December 2013 09:13 |
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 |
|
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
|
|
|