Reputation: 155
There is a table which has a CLOB JSON column:
CREATE TABLE STUDENT
(
NAME VARCHAR2(32 BYTE),
ADDRESS VARCHAR2(30 BYTE),
PAYMENT_JS CLOB
);
ALTER TABLE STUDENT ADD
CONSTRAINT CK_PAYMENT_JS
CHECK ("PAYMENT_JS" IS JSON (LAX));
PAYMENT_JS is a HUGECLOB which has JSON data of the below format:
{
"pays": [{
"payCode": {
"code": "OTHER"
},
"payValue": {
"amount": 0.0,
"currencyCode": "USD"
}
},
{
"payCode": {
"code": "COST_OF_STUDY"
},
"payValue": {
"amount": 2395.29,
"currencyCode": "USD"
}
}]
}
I'm using JSON_TABLE to get data from PAYMENT_JS column and other columns in the table:
select
e.name AS stud_name,
e.address AS stud_address,
j.code
FROM
STUDENT e,
JSON_TABLE ( payment_js,'$.pays[*]'
COLUMNS (
code VARCHAR2 ( 100 CHAR ) PATH '$.payCode.code' ,
amount NUMBER ( 10,2 ) PATH '$.payValue.amount' ,
currencycode VARCHAR2 ( 4 CHAR ) PATH '$.payValue.currencyCode'
)
)
j
But when the PAYMENT_JS column doesn't have data, those rows' NAME and ADDRESS are also not getting retrieved.
How do we retrieve data for other columns in the table when the JSON column is empty?
I tried NULL ON EMPTY but doesnt work:
select
e.name AS stud_name,
e.address AS stud_address,
j.code
FROM
STUDENT e,
JSON_TABLE ( payment_js,'$.pays[*]'
COLUMNS (
code VARCHAR2 ( 100 CHAR ) PATH '$.payCode.code' NULL ON EMPTY,
amount NUMBER ( 10,2 ) PATH '$.payValue.amount' NULL ON EMPTY,
currencycode VARCHAR2 ( 4 CHAR ) PATH '$.payValue.currencyCode' NULL ON EMPTY
)
)
j
My output should look like this:
stud_name, stud_address,code
-----------------------------
Maria, 1 Avenue NY,OTHER
David, 2 Avenue NY,COST_OF_STUDY
Kavya, 3 Avenue NY
Here, the JSON column is empty for Kavya.
But Oracle returns the below output for the query:
stud_name, stud_address,code
-----------------------------
Maria, 1 Avenue NY,OTHER
David, 2 Avenue NY,COST_OF_STUDY
I'm using Oracle Database 12c Enterprise Edition Release 12.2.0.1.0
Upvotes: 1
Views: 2433
Reputation: 3212
TableA, TableB syntax is old syntax for cartesian products, so every table must return at least 1 row, but since the second table is derived from the first one, it's a little bit odd, try with left join
select
e.name AS stud_name,
e.address AS stud_address,
j.code
FROM
STUDENT e
left join (
JSON_TABLE ( payment_js,'$.pays[*]'
COLUMNS (
code VARCHAR2 ( 100 CHAR ) PATH '$.payCode.code' NULL ON EMPTY,
amount NUMBER ( 10,2 ) PATH '$.payValue.amount' NULL ON EMPTY,
currencycode VARCHAR2 ( 4 CHAR ) PATH '$.payValue.currencyCode' NULL ON EMPTY
)
) j
on 1 = 1
Upvotes: 3