Choose separate elements while attaching two XML types
source link: https://www.codesd.com/item/choose-separate-elements-while-attaching-two-xml-types.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Choose separate elements while attaching two XML types
I have a table ATTR_MASTER
which has contents as follows:
ID L_NAME_N C_LEVEL
1000 e1 D
1001 e2 D
1002 e3 D
1003 e4 D
1004 e1 D
1005 e2 D
1006 e3 D
1007 e4 D
Now I am executing select query as follows:
SELECT e.ID,x.EVALUE,z.VALUE_TYPE,x.w FROM ATTR_MASTER e, xmltable
(
'//B/C/D/*[local-name(.)!=''w'' and text()]'
PASSING xmltype('<A><B><C><D><w>1L</w><e1>AMAR</e1><e2>AKBAR</e2><e3>1234</e3><e4>BIJAY</e4></D></C><C><D><w>1B</w><e1>ARTI</e1><e2>AKBAR</e2><e3>5678</e3><e4>BIJAY</e4></D></C></B></A>')
COLUMNS
EVALUE VARCHAR2(100) PATH './text()',
L_NAME_EN VARCHAR2(50) PATH 'local-name(.)',
w VARCHAR2(20) PATH './parent::*/w'
)x LEFT OUTER JOIN
XMLTABLE
(
'//GetDataLookupValuesResponse/tuple/old'
PASSING xmltype('<GetDataLookupValuesResponse><tuple><old><DataLookup><Key>e1</Key><Value>String</Value></DataLookup></old></tuple><tuple><old><DataLookup><Key>e2</Key><Value>String</Value></DataLookup></old></tuple><tuple><old><DataLookup><Key>e3</Key><Value>Number</Value></DataLookup></old></tuple><tuple><old><DataLookup><Key>e4</Key><Value>String</Value></DataLookup></old></tuple></GetDataLookupValuesResponse>')
COLUMNS
VALUE_TYPE VARCHAR2(50) PATH '//Value',
C_KEY varchar2(50) PATH '//Key'
)z ON x.L_NAME_EN=z.C_KEY
where e.L_NAME_EN=x.L_NAME_EN;
The output shown is
ID EVALUE VALUE_TYPE W
1004 AMAR String 1L
1000 AMAR String 1L
1005 AKBAR String 1L
1001 AKBAR String 1L
1006 1234 Number 1L
1002 1234 Number 1L
1007 BIJAY String 1L
1003 BIJAY String 1L
1004 ARTI String 1B
1000 ARTI String 1B
1005 AKBAR String 1B
1001 AKBAR String 1B
1006 5678 Number 1B
1002 5678 Number 1B
1007 BIJAY String 1B
1003 BIJAY String 1B
This is because we have duplicate elements in the column L_NAME_N
of the table ATTR_MASTER
, that is why in the where clause of the select statement, each element is considered twice and again since in the XML
, we have two "w" elements, the value of each element is printed four times in the output. But I need the following result,
ID EVALUE VALUE_TYPE W
1000 AMAR String 1L
1004 ARTI String 1B
1001 AKBAR String 1L
1005 AKBAR String 1B
1002 1234 Number 1L
1006 5678 Number 1B
1003 BIJAY String 1L
1007 BIJAY String 1B
The sequence of the rows may vary in the output which is not a issue, but I need only these rows. Again I cannot change the contents of the table ATTR_MASTER
. Also I have tried SELECT DISTINCT...
, but it won't work. Where am I wrong?
The DISTINCT won't work because ID is different, do you need the ID field?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK