[英]ORACLE JSON_TABLE I need to get 2 columns from an array
I have this SQL code我有这个 SQL 代码
SELECT * FROM JSON_TABLE (('
[
"tipodenunciaid": [1,2],
"municipioid": [1,2]
]
'), '$[*]' COLUMNS (tipodenunciaid integer PATH '$.tipodenunciaid[*]', municipioid integer PATH '$.municipioid[*]'));
And I want to get the next result:我想得到下一个结果:
What's wrong with my query?我的查询有什么问题?
Thank you so much.太感谢了。
You can't use json_table()
like that.你不能这样使用
json_table()
。 If you want both arrays unnested in different columns, and lined up according to the position of each element in each array, then one option would be to separately expand each array to rows, using ordinality
to keep track of the position of each element, then join the two resultsets.如果您希望 arrays 都未嵌套在不同的列中,并根据每个数组中每个元素的 position 排列,那么一个选项是将每个数组单独扩展为行,使用
ordinality
来跟踪每个元素的 Z4757FE07FD492A8BEEEZA6 然后加入两个结果集。 A full join
comes handy to accommodate for an unequal number of elements in each array: full join
可以方便地容纳每个数组中不相等数量的元素:
with
data as (
select '{ "tipodenunciaid": [1,2,3,4], "municipioid": [1,2,3,4,5] }' as js from dual
),
t as (
select t.*
from data d
cross apply json_table(
d.js,
'$.tipodenunciaid[*]' columns (rn for ordinality, tipodenunciaid integer path '$')
) t
),
m as (
select m.*
from data d
cross apply json_table(
d.js,
'$.municipioid[*]' columns (rn for ordinality, municipioid integer path '$')
) m
)
select t.tipodenunciaid, m.municipioid
from t
full join m on m.rn = t.rn
You can use two OUTER APPLY
s with FOR ORDINALITY
to generate the ordinates of the array to connect the table to itself:您可以将两个
OUTER APPLY
与FOR ORDINALITY
一起使用来生成数组的纵坐标以将表连接到自身:
SELECT j.id,
CASE
WHEN t.id = m.id OR t.id > m.max_id OR m.id IS NULL
THEN tipodenunciaid
END AS tipodenunciaid,
CASE
WHEN t.id = m.id OR m.id > t.max_id OR t.id IS NULL
THEN municipioid
END AS municipioid
FROM table_name j
OUTER APPLY (
SELECT id,
tipodenunciaid,
MAX(id) OVER () AS max_id
FROM JSON_TABLE(
j.json,
'$.tipodenunciaid[*]'
COLUMNS (
id FOR ORDINALITY,
tipodenunciaid integer PATH '$'
)
)
) t
OUTER APPLY (
SELECT id,
municipioid,
MAX(id) OVER () AS max_id
FROM JSON_TABLE(
j.json,
'$.municipioid[*]'
COLUMNS (
id FOR ORDINALITY,
municipioid integer PATH '$'
)
)
) m
WHERE t.id = m.id
OR t.id IS NULL
OR ( m.id > t.max_id AND t.id = t.max_id )
OR m.id IS NULL
OR ( t.id > m.max_id AND m.id = m.max_id );
Which, for the sample data:其中,对于样本数据:
CREATE TABLE table_name ( id, json ) AS
SELECT 1, '{"tipodenunciaid":[1,2],"municipioid":[1,2]}' FROM DUAL UNION ALL
SELECT 2, '{"tipodenunciaid":[],"municipioid":[3,4]}' FROM DUAL UNION ALL
SELECT 3, '{"tipodenunciaid":[5],"municipioid":[]}' FROM DUAL UNION ALL
SELECT 4, '{"tipodenunciaid":[6,7],"municipioid":[6]}' FROM DUAL UNION ALL
SELECT 5, '{"tipodenunciaid":[8],"municipioid":[8,9]}' FROM DUAL;
Outputs:输出:
ID |身份证 | TIPODENUNCIAID |
TIPODENUNCIAID | MUNICIPIOID -: |
MUNICIPIOID -: | -------------: |
-------------: | ----------: 1 |
----------: 1 | 1 |
1 | 1 1 |
1 1 | 2 |
2 | 2 2 |
2 2 | null |
null | 3 2 |
3 2 | null |
null | 4 3 |
4 3 | 5 |
5 | null 4 |
null 4 | 6 |
6 | 6 4 |
6 4 | 7 |
7 | null 5 |
null 5 | 8 |
8 | 8 5 |
8 5 | null |
null | 9
9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.