我希望在单个查询中获取与文档相关的所有元数据值,在某些列中为null。
示例:
DOCUMENT table ATTRIBUTE_1 values ATTRIBUTE_2 values
Id | Name DocId | Value DocId | Value
----------- --------------- ---------------
1 | Doc1 1 | Val1 1 | ValA
2 | Doc2 1 | Val2 1 | ValB
1 | ValC
对于Doc1,我希望查询返回按字母顺序排序的每个属性的值:
Attr1 | Attr2
--------------------
Val1 | ValA
Val2 | ValB
NULL | ValC
我尝试了一个非常天真的问题:
SELECT a1.Value, a2.Value FROM ATTRIBUTE_1 a1, ATTRIBUTE_2 a2, DOCUMENT d
WHERE d.Id = a1.DocId AND d.Id = a2.DocId AND d.Id = 1
我尝试进行内部联接,我尝试使用Google搜索,但找不到与单个多个列合并的条款。
使用的数据库是Oracle。
我如何实现这一目标?
谢谢
答案 0 :(得分:1)
这对我有用:
with a1 as (
select
row_number() over (partition by DocId order by Value) as r,
DocId,
value
from
ATTRIBUTE_1
), a2 as (
select
row_number() over (partition by DocId order by Value) as r,
DocId,
value
from
ATTRIBUTE_2
)
select a1.Value as Attr1, a2.Value as Attr2
from a1 full outer join a2 using (DocId, r) join DOCUMENT on (DocId = Id);
请参阅此SQL小提琴:http://sqlfiddle.com/#!4/a3526/3/0