how to give column values as xml element names in Oracle - sql

I have two tables. I am trying to join them and prepare an XML by using SQL/XML (SQLX) in oracle. Here the problem is XMLELEMENT function is taking hardcoded values for element name, but I want to have those names as a column data. Is it possible?
create table PRODUCTEDIT
(
PRODUCTEDIT_NUM NUMBER(12) primary key,
API_NAME VARCHAR2(255)
);
create table PRODUCTEDITPARAMETER
(
PRODUCTEDIT_NUM NUMBER(12) not null,
PARAMETER_SEQ NUMBER(9) not null,
PARAMETER_VALUE VARCHAR2(4000),
CONSTRAINT fk_producteditparameter
FOREIGN KEY (PRODUCTEDIT_NUM )
REFERENCES PRODUCTEDIT(PRODUCTEDIT_NUM )
);
There are 2 records in first table.
PRODUCTEDIT_NUM Api_Name
1 ModifyProd
2 CreateProd
Records in second table:
PRODUCTEDIT_NUM PARAMETER_SEQ PARAMETER_VALUE
1 1 10
1 2 Data
1 3 1
1 4 Data1
1 5 1
2 1 11
2 2 Voice
2 3 1
Now I want to get XMLOUTPUT like below:
<?xml version='1.0'?>
<ModifyProd>
<1>10</1>
<2>Data</2>
<3>1</3>
<4>Data1</4>
<5>1</5>
</ModifyProd>
<CreateProd>
<1>11</1>
<2>Voice</2>
<3>1</3>
</CreateProd>
In above XML we have XMLELEMENT names (ModifyProd,CreateProd, 1,2 e.t.c) coming from table data.I am not able to achieve that by using SQLXML in oracle.
I tried below, but doesn't seem to be working. XMLELEMENT is not taking the value the way I am passing.
SELECT XMLROOT(
XMLELEMENT(d.api_name,
(SELECT XMLAGG(
XMLELEMENT(e.parameter_seq,e.parameter_value
)
)
FROM producteditparameter e
WHERE e.productedit_num = d.productedit_num
)
),version '1.0', standalone yes
)
FROM productedit d

I assume you are looking for this:
WITH t AS
(SELECT 'foo' AS ELEMENT_NAME, 'bar' AS ELEMENT_CONTENT FROM dual)
SELECT XMLELEMENT(EVALNAME ELEMENT_NAME, ELEMENT_CONTENT)
FROM t;
<foo>bar</foo>
Update based on additional input
Your result is not a well-formed XML. A XML document must have only one single root element. So, either you ask for several XML documents, then you can do this:
SELECT
XMLELEMENT(
EVALNAME Api_Name,
XMLAGG(XMLELEMENT(EVALNAME parameter_seq, e.parameter_value) ORDER BY parameter_seq)
) AS xml_result
FROM PRODUCTEDITPARAMETER e
JOIN PRODUCTEDIT d USING (productedit_num)
GROUP BY productedit_num, Api_Name;
<ModifyProd><1>10</1><2>Data</2><3>1</3><4>Data1</4><5>1</5></ModifyProd>
<CreateProd><1>11</1><2>Voice</2><3>1</3></CreateProd>
or if you need a single XML you have to enclose it by another element, e.g.
SELECT
XMLELEMENT("Products",
XMLAGG(
XMLELEMENT(
EVALNAME Api_Name,
XMLAGG(XMLELEMENT(EVALNAME parameter_seq, e.parameter_value) ORDER BY parameter_seq)
)
)
) AS xml_result
FROM PRODUCTEDITPARAMETER e
JOIN PRODUCTEDIT d USING (productedit_num)
GROUP BY productedit_num, Api_Name;
<Products><ModifyProd><1>10</1><2>Data</2><3>1</3><4>Data1</4><5>1</5></ModifyProd><CreateProd><1>11</1><2>Voice</2><3>1</3></CreateProd></Products>

Another solution if I understood what you want:
WITH t AS
( SELECT LEVEL col1, 'test' || LEVEL col2
FROM DUAL
CONNECT BY LEVEL < 10)
SELECT XMLSERIALIZE (DOCUMENT (xmltype (CURSOR (SELECT col1, col2 FROM t))) INDENT SIZE = 0)
FROM DUAL;
Then, you can use a XSD to transform the output as you want.
Hope it helps

Related

How to count how many times a tag appear inside a CLOB PLSQL oracle 11g

i have a clob column in a table. In this clob i have a XML and i want to count how many times a tag appear inside this clob.
For example:
<TPQ>
<LTP>N<LTP>
<SUBLTP>N</SUBLTP>
<TIMES>446</TIMES>
<TIMES>321</TIMES>
<TIMES>546</TIMES>
<TIMES>547</TIMES>
<LTP>N</LTP>
<LTP2>N<LTP2>
<SUBLTP>N</SUBLTP>
<NODES>1</NODES>
<NODES>2</NODES>
<NODES>3</NODES>
<NODES>4</NODES>
<SUBLTP>H</SUBLTP>
<SUBLTP3>A</SUBLTP3>
<SUBLTP2>N</SUBLTP2>
<LTP2>N</LTP2>
</TPQ>
I want to know that the tag "TIMES" appears 4 times, and tag "NODES" appears 4 times.
Im using this query for getting all TIMES tag but i need know how to count:
SELECT EXTRACT(xmltype.createxml(T.columnCLOB), '//TPQ/LTP/TIMES').getStringVal()
FROM table1 T;
and the result is this:
Result of the Select Statement is
<TIMES>446</TIMES><TIMES>321</TIMES><TIMES>546</TIMES><TIMES>547</TIMES>
This in a example, i need a solution for a dinamic clob column that can have x tags inside, not always with the same structure. But i only need to know how many times appears a specified tag.
You can use:
SELECT t.id,
x.tag_name,
COUNT(*)
FROM table_name t
CROSS JOIN XMLTABLE(
'//*'
PASSING XMLTYPE(t.xml)
COLUMNS
tag_name varchar2(100) path 'name()'
) x
GROUP BY t.id, x.tag_name
Which, for the sample data:
CREATE TABLE table_name (id NUMBER, xml CLOB);
INSERT INTO table_name (id, xml)
VALUES (1, '<TPQ>
<LTP>N</LTP>
<SUBLTP>N</SUBLTP>
<TIMES>446</TIMES>
<TIMES>321</TIMES>
<TIMES>546</TIMES>
<TIMES>547</TIMES>
<LTP>N</LTP>
<LTP2>N</LTP2>
<SUBLTP>N</SUBLTP>
<NODES>1</NODES>
<NODES>2</NODES>
<NODES>3</NODES>
<NODES>4</NODES>
<SUBLTP>H</SUBLTP>
<SUBLTP3>A</SUBLTP3>
<SUBLTP2>N</SUBLTP2>
<LTP2>N</LTP2>
</TPQ>');
Outputs:
ID
TAG_NAME
COUNT(*)
1
LTP
2
1
LTP2
2
1
SUBLTP2
1
1
NODES
4
1
TPQ
1
1
SUBLTP
3
1
TIMES
4
1
SUBLTP3
1
If you only want a specific tag name and want to aggregate the tags' contents then:
SELECT t.id,
x.tag_name,
COUNT(*),
LISTAGG(x.value, ',') WITHIN GROUP (ORDER BY value) AS contents
FROM table_name t
CROSS JOIN XMLTABLE(
'//TIMES'
PASSING XMLTYPE(t.xml)
COLUMNS
tag_name VARCHAR2(100) PATH 'name()',
value VARCHAR2(4000) PATH 'text()'
) x
GROUP BY t.id, x.tag_name
Which outputs:
ID
TAG_NAME
COUNT(*)
CONTENTS
1
TIMES
4
321,446,546,547
db<>fiddle here
XPATH functions can be used
with
x as
(select xmltype('<TPQ><LTP>N</LTP><SUBLTP>N</SUBLTP>
<TIMES>446</TIMES><TIMES>321</TIMES><TIMES>546</TIMES><TIMES>547</TIMES>
<LTP>N</LTP><LTP2>N</LTP2><SUBLTP>N</SUBLTP>
<NODES>1</NODES><NODES>2</NODES><NODES>3</NODES><NODES>4</NODES>
<SUBLTP>H</SUBLTP><SUBLTP3>A</SUBLTP3><SUBLTP2>N</SUBLTP2><LTP2>N</LTP2></TPQ>') xval
from dual)
select z.*
from x, xmltable ('count(/TPQ/TIMES)' passing x.xval) z;

Need Sorting With External Array or Comma Separated data

Am working with PostgreSQL 8.0.2, I have table
create table rate_date (id serial, rate_name text);
and it's data is
id rate_name
--------------
1 startRate
2 MidRate
3 xlRate
4 xxlRate
After select it will show data with default order or order by applied to any column of same table. My requirement is I have separate entity from where I will get data as (xlRate, MidRate,startRate,xxlRate) so I want to use this data to sort the select on table rate_data. I have tried for values join but it's not working and no other solution am able to think will work. If any one have idea please share detail.
Output should be
xlRate
MidRate
startRate
xxlRate
my attempt/thinking.
select id, rate_name
from rate_date r
join (
VALUES (1, 'xlRate'),(2, 'MidRate')
) as x(a,b) on x.b = c.rate_name
I am not sure if this is helpful but in Oracle you could achieve that this way:
select *
from
(
select id, rate_name,
case rate_name
when 'xlRate' then 1
when 'MidRate' then 2
when 'startRate' then 3
when 'xxlRate' then 4
else 100
end my_order
from rate_date r
)
order by my_order
May be you can do something like this in PostgreSQL?

Unexpected behavior in Oracle when using Group By with JSON_TABLE

I have a denormalized VIEW we'll call VIEW_X which looks like the following (It's just a regular simple view - not materialized or anything like that):
ID GROUP_ID PART_1_ID PART_2_ID
1 1723189 cd69f0f4-a5ed-4196-916d-401e98ffec75 X1
1 1723189 cd69f0f4-a5ed-4196-916d-401e98ffec75 X2
2 1723185 8d5132cb-1b6e-4e79-9698-fd1962eb808f K1
2 1723188 a191cb01-32ac-4ab4-bd6b-3ef777e395ca K1
It's denormalized in that it actually represents a structure like this:
{
id: 1,
group_id: 1723189,
part_1_id: 'cd69f0f4-a5ed-4196-916d-401e98ffec75'
part_2_ids: ["X1", "X2"]
}
the PART_2_ID in this view is the result of selecting from a JSON_TABLE where the data in the original table is stored in an array like ["X1", "X2"]:
JSON_TABLE(a.PART_2_IDS, '$' COLUMNS (
NESTED PATH '$[*]'
COLUMNS (
PART_2_ID VARCHAR2(4000) PATH '$'
)
)) p2
When I run a query like this on this view I get 0 results although the expected result is a single result with the ID of 2:
SELECT ID
FROM VIEW_X
WHERE PART_2_ID IN ('K1')
GROUP BY ID
HAVING COUNT(DISTINCT(PART_2_ID)) = 1
ID
--
(no results)
figure 1
Curiously enough if I run just the following I get the expected two results as there are two rows with ID 2 where there is a match on PART_2_ID as K1:
SELECT ID
FROM VIEW_X
WHERE PART_2_ID IN ('K1')
ID
--
2
2
If, however, I run either of the following queries I get a match on ID 1:
SELECT ID
FROM VIEW_X
WHERE PART_2_ID IN ('X1')
GROUP BY ID
HAVING COUNT(DISTINCT(PART_2_ID)) = 1
ID
--
1
SELECT ID
FROM VIEW_X
WHERE PART_2_ID IN ('X1', 'X2')
GROUP BY ID
HAVING COUNT(DISTINCT(PART_2_ID)) = 2
ID
--
1
I don't understand why figure 1 is not returning the expected result - is there something I'm overlooking? Is this a quirk with how JSON_TABLE works?
I cannot replicate this in:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production; or
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production on https://livesql.oracle.com
Oracle Setup:
CREATE TABLE table1 ( document CLOB CONSTRAINT ensure_json CHECK (document IS JSON) );
INSERT INTO table1 ( document ) VALUES ( '{"id":1,"group_id":1723189,"part_1_id":"cd69f0f4-a5ed-4196-916d-401e98ffec75","part_2_ids":["X1","X2"]}' );
INSERT INTO table1 ( document ) VALUES ( '{"id":2,"group_id":1723185,"part_1_id":"8d5132cb-1b6e-4e79-9698-fd1962eb808f","part_2_ids":["K1"]}' );
INSERT INTO table1 ( document ) VALUES ( '{"id":2,"group_id":1723188,"part_1_id":"a191cb01-32ac-4ab4-bd6b-3ef777e395ca","part_2_ids":["K1"]}' );
CREATE VIEW VIEW_X AS
SELECT p.*
FROM table1 t
CROSS JOIN
JSON_TABLE(
t.document,
'$'
COLUMNS (
id PATH '$.id',
group_id PATH '$.group_id',
part_1_id PATH '$.part_1_id',
NESTED PATH '$.part_2_ids[*]'
COLUMNS (
PART_2_ID VARCHAR2(4000) PATH '$'
)
)
) p;
Query 1:
SELECT *
FROM VIEW_X;
Results:
ID GROUP_ID PART_1_ID PART_2_ID
---------- ---------- ------------------------------------ ---------
1 1723189 cd69f0f4-a5ed-4196-916d-401e98ffec75 X1
1 1723189 cd69f0f4-a5ed-4196-916d-401e98ffec75 X2
2 1723185 8d5132cb-1b6e-4e79-9698-fd1962eb808f K1
2 1723188 a191cb01-32ac-4ab4-bd6b-3ef777e395ca K1
Query 2:
SELECT ID
FROM VIEW_X
WHERE PART_2_ID IN ('K1')
GROUP BY ID
HAVING COUNT(DISTINCT(PART_2_ID)) = 1;
Results:
ID
--
2

Datatype in sql developer is opaque

So I am debugging a part of my code. I created datatype called array2D. which is a table of number_array which is table of number.
create or replace type NUMBER_ARR is table of NUMBER;
create or replace TYPE ARRAY2D AS TABLE OF NUMBER_ARR;
I compile both types for debug and after doing so i am able to see each row of the table. But not individual elements stored in it. Element's type is listed as opaque. I had this for the whole table before I compiled datatype for debug.
create or replace type NUMBER_ARR is table of NUMBER;
/
create or replace TYPE ARRAY2D AS TABLE OF NUMBER_ARR;
/
create table t (id integer primary key, n array2d)
nested table n store as n_a(nested table column_value store as n_c);
insert into t values(1, array2d(number_arr(1,2), number_arr(3,4)));
insert into t values(2, array2d(number_arr(1,4)));
Select * from t displays:
ID N
----------------------------
1 [unsupported data type]
2 [unsupported data type]
Now, to display the values, you have to convert each element into table and join like so:
select t1.id, t3.column_value value
from t t1, table(t1.column_value) t2, table(t2.column_value) t3;
ID value
----------------
1 1
1 2
1 3
1 4
2 1
2 4
To display all the values per ID, you may use listagg
select t1.id,
listagg(t3.column_value, ', ')
within group (order by t3.column_value) value
from t t1, table(t1.column_value) t2, table(t2.column_value) t3
group by t1.id;
ID value
----------------
1 1, 2, 3, 4
2 1, 4
Ofcourse, it's displaying all the element of 2d array as single string of numbers.
The proper display (i.e. each array inside its own brackets like ((1,2,3,4),(4,5,6))) is not possible as per your current type definitions.
This doesn't work:
select id, '(' || listagg(value, ',')
within group (order by value) || ')' from
(select t1.id, t2.column_value c, '(' || listagg(t3.column_value, ',')
within group (order by t3.column_value) ||')' value
from t t1, table(t1.column_value) t2, table(t2.column_value) t3
group by t1.id, c)
group by id;
To make query like above work, you have to define object type and define map and order functions.
Lastly, I want to say you should stick with regular datatypes as they much more efficient.
P.S. - If there is no unique key, then you have to use rownum or rowid.

Is it possible to concatenate column values into a string using CTE?

Say I have the following table:
id|myId|Name
-------------
1 | 3 |Bob
2 | 3 |Chet
3 | 3 |Dave
4 | 4 |Jim
5 | 4 |Jose
-------------
Is it possible to use a recursive CTE to generate the following output:
3 | Bob, Chet, Date
4 | Jim, Jose
I've played around with it a bit but haven't been able to get it working. Would I do better using a different technique?
I do not recommend this, but I managed to work it out.
Table:
CREATE TABLE [dbo].[names](
[id] [int] NULL,
[myId] [int] NULL,
[name] [char](25) NULL
) ON [PRIMARY]
Data:
INSERT INTO names values (1,3,'Bob')
INSERT INTO names values 2,3,'Chet')
INSERT INTO names values 3,3,'Dave')
INSERT INTO names values 4,4,'Jim')
INSERT INTO names values 5,4,'Jose')
INSERT INTO names values 6,5,'Nick')
Query:
WITH CTE (id, myId, Name, NameCount)
AS (SELECT id,
myId,
Cast(Name AS VARCHAR(225)) Name,
1 NameCount
FROM (SELECT Row_number() OVER (PARTITION BY myId ORDER BY myId) AS id,
myId,
Name
FROM names) e
WHERE id = 1
UNION ALL
SELECT e1.id,
e1.myId,
Cast(Rtrim(CTE.Name) + ',' + e1.Name AS VARCHAR(225)) AS Name,
CTE.NameCount + 1 NameCount
FROM CTE
INNER JOIN (SELECT Row_number() OVER (PARTITION BY myId ORDER BY myId) AS id,
myId,
Name
FROM names) e1
ON e1.id = CTE.id + 1
AND e1.myId = CTE.myId)
SELECT myID,
Name
FROM (SELECT myID,
Name,
(Row_number() OVER (PARTITION BY myId ORDER BY namecount DESC)) AS id
FROM CTE) AS p
WHERE id = 1
As requested, here is the XML method:
SELECT myId,
STUFF((SELECT ',' + rtrim(convert(char(50),Name))
FROM namestable b
WHERE a.myId = b.myId
FOR XML PATH('')),1,1,'') Names
FROM namestable a
GROUP BY myId
A CTE is just a glorified derived table with some extra features (like recursion). The question is, can you use recursion to do this? Probably, but it's using a screwdriver to pound in a nail. The nice part about doing the XML path (seen in the first answer) is it will combine grouping the MyId column with string concatenation.
How would you concatenate a list of strings using a CTE? I don't think that's its purpose.
A CTE is just a temporarily-created relation (tables and views are both relations) which only exists for the "life" of the current query.
I've played with the CTE names and the field names. I really don't like reusing fields names like id in multiple places; I tend to think those get confusing. And since the only use for names.id is as a ORDER BY in the first ROW_NUMBER() statement, I don't reuse it going forward.
WITH namesNumbered as (
select myId, Name,
ROW_NUMBER() OVER (
PARTITION BY myId
ORDER BY id
) as nameNum
FROM names
)
, namesJoined(myId, Name, nameCount) as (
SELECT myId,
Cast(Name AS VARCHAR(225)),
1
FROM namesNumbered nn1
WHERE nameNum = 1
UNION ALL
SELECT nn2.myId,
Cast(
Rtrim(nc.Name) + ',' + nn2.Name
AS VARCHAR(225)
),
nn.nameNum
FROM namesJoined nj
INNER JOIN namesNumbered nn2 ON nn2.myId = nj.myId
and nn2.nameNum = nj.nameCount + 1
)
SELECT myId, Name
FROM (
SELECT myID, Name,
ROW_NUMBER() OVER (
PARTITION BY myId
ORDER BY nameCount DESC
) AS finalSort
FROM namesJoined
) AS tmp
WHERE finalSort = 1
The first CTE, namesNumbered, returns two fields we care about and a sorting value; we can't just use names.id for this because we need, for each myId value, to have values of 1, 2, .... names.id will have 1, 2 ... for myId = 1 but it will have a higher starting value for subsequent myId values.
The second CTE, namesJoined, has to have the field names specified in the CTE signature because it will be recursive. The base case (part before UNION ALL) gives us records where nameNum = 1. We have to CAST() the Name field because it will grow with subsequent passes; we need to ensure that we CAST() it large enough to handle any of the outputs; we can always TRIM() it later, if needed. We don't have to specify aliases for the fields because the CTE signature provides those. The recursive case (after the UNION ALL) joins the current CTE with the prior one, ensuring that subsequent passes use ever-higher nameNum values. We need to TRIM() the prior iterations of Name, then add the comma and the new Name. The result will be, implicitly, CAST()ed to a larger field.
The final query grabs only the fields we care about (myId, Name) and, within the subquery, pointedly re-sorts the records so that the highest namesJoined.nameCount value will get a 1 as the finalSort value. Then, we tell the WHERE clause to only give us this one record (for each myId value).
Yes, I aliased the subquery as tmp, which is about as generic as you can get. Most SQL engines require that you give a subquery an alias, even if it's the only relation visible at that point.