如何 Select 结点 table 中任何给定 ID 的所有可能的不同相关 "two-ways" 值?
How to Select all possible distinct related "two-ways" values for any given ID from a junction table?
table TBL_IN "输入文档详细信息 table"
IID
IDate
ISubject
I-225
I-300
I-410
tableTBL_OUT“OUT文档详细信息table”
OID
ODate
OSubject
O-20
O-35
table TBL_INOUT "IN OUT 文档连接点 table"
IOID
IID
OID
1
I-225
O-20
2
I-225
O-35
3
I-300
O-35
4
I-410
O-20
问题是如何在 oracle sql 或从结点 table 访问查询中得到以下查询结果?
查询QRY_INOUT_Related“Select任何给定 ID 的所有可能相关的“双向”值”
ID
IO_Related
O-20
I-225,I410,O-35,I-300
ID
IO_Related
I-300
O-35,I-225,O-20,I-410
我希望我尽可能地阐明了这个想法。
在 Oracle 中,您似乎想要一个分层查询,它通过 IID
或 OID
找到所有不同的连接,然后想要聚合:
SELECT id,
LISTAGG(value, ',') WITHIN GROUP (ORDER BY value) AS io_related
FROM (
SELECT DISTINCT id, value
FROM (
SELECT CONNECT_BY_ROOT oid AS id,
iid,
oid
FROM tbl_inout
START WITH oid = 'O-20'
CONNECT BY NOCYCLE
PRIOR oid = oid
OR PRIOR iid = iid
)
UNPIVOT(value FOR key IN (iid, oid))
WHERE id <> value
)
GROUP BY id
输出:
ID
IO_RELATED
O-20
I-225,I-300,I-410,O-35
和
SELECT id,
LISTAGG(value, ',') WITHIN GROUP (ORDER BY value) AS io_related
FROM (
SELECT DISTINCT id, value
FROM (
SELECT CONNECT_BY_ROOT iid AS id,
iid,
oid
FROM tbl_inout
START WITH iid = 'I-300'
CONNECT BY NOCYCLE
PRIOR oid = oid
OR PRIOR iid = iid
)
UNPIVOT(value FOR key IN (iid, oid))
WHERE id <> value
)
GROUP BY id
输出:
ID
IO_RELATED
I-300
I-225,I-410,O-20,O-35
db<>fiddle here
table TBL_IN "输入文档详细信息 table"
IID | IDate | ISubject |
---|---|---|
I-225 | ||
I-300 | ||
I-410 |
tableTBL_OUT“OUT文档详细信息table”
OID | ODate | OSubject |
---|---|---|
O-20 | ||
O-35 |
table TBL_INOUT "IN OUT 文档连接点 table"
IOID | IID | OID |
---|---|---|
1 | I-225 | O-20 |
2 | I-225 | O-35 |
3 | I-300 | O-35 |
4 | I-410 | O-20 |
问题是如何在 oracle sql 或从结点 table 访问查询中得到以下查询结果?
查询QRY_INOUT_Related“Select任何给定 ID 的所有可能相关的“双向”值”
ID | IO_Related |
---|---|
O-20 | I-225,I410,O-35,I-300 |
ID | IO_Related |
---|---|
I-300 | O-35,I-225,O-20,I-410 |
我希望我尽可能地阐明了这个想法。
在 Oracle 中,您似乎想要一个分层查询,它通过 IID
或 OID
找到所有不同的连接,然后想要聚合:
SELECT id,
LISTAGG(value, ',') WITHIN GROUP (ORDER BY value) AS io_related
FROM (
SELECT DISTINCT id, value
FROM (
SELECT CONNECT_BY_ROOT oid AS id,
iid,
oid
FROM tbl_inout
START WITH oid = 'O-20'
CONNECT BY NOCYCLE
PRIOR oid = oid
OR PRIOR iid = iid
)
UNPIVOT(value FOR key IN (iid, oid))
WHERE id <> value
)
GROUP BY id
输出:
ID IO_RELATED O-20 I-225,I-300,I-410,O-35
和
SELECT id,
LISTAGG(value, ',') WITHIN GROUP (ORDER BY value) AS io_related
FROM (
SELECT DISTINCT id, value
FROM (
SELECT CONNECT_BY_ROOT iid AS id,
iid,
oid
FROM tbl_inout
START WITH iid = 'I-300'
CONNECT BY NOCYCLE
PRIOR oid = oid
OR PRIOR iid = iid
)
UNPIVOT(value FOR key IN (iid, oid))
WHERE id <> value
)
GROUP BY id
输出:
ID IO_RELATED I-300 I-225,I-410,O-20,O-35
db<>fiddle here