gpt4 book ai didi

sql - Oracle 外连接 "entity"

转载 作者:行者123 更新时间:2023-12-01 15:34:25 24 4
gpt4 key购买 nike

如何在查询中引用从外部联接创建的“组合实体”?具体来说,如何替换“??”在下面的查询中:

SELECT TableA.x, 
??.y --How do you select from the combined entity?
FROM TableA,
TableB buys
FULL OUTER JOIN TableB sells ON buys.run_id = sells.run_id
AND buys.specie_id = sells.specie_id
AND buys.account_id = sells.account_id
WHERE TableA.id = ??
AND -- want to join this with the "combined entity" resulting from the outer join
buys.buy = 'Y'
AND -- Is this valid or does this have to be within the Outer join statement?
sells.buy = 'N';

最佳答案

select TableA.x, fullTable.y 
from TableA
INNER JOIN
( SELECT y
FROM TableB buys
full outer join TableB sells
on buys.run_id = sells.run_id
and buys.specie_id = sells.specie_id
and buys.account_id = sells.account_id
AND buys.buy = 'Y' AND sells.buy = 'N'
) AS fullTable
ON TableA.id = fullTable.y

条件可以是最终的WHEREON像这样,但它本质上取消了完整的连接:

select TableA.x, fullTable.y 
from TableA
INNER JOIN
( SELECT y
, buys.buy AS buysBuy --- fields here so they can be
, sells.buy AS sellsBuy --- used in the final WHERE or ON
FROM TableB buys
full outer join TableB sells
on buys.run_id = sells.run_id
and buys.specie_id = sells.specie_id
and buys.account_id = sells.account_id
) AS fullTable
ON TableA.id = fullTable.y
AND buysBuy = 'Y' AND sellsBuy = 'N' --- the condition here

关于sql - Oracle 外连接 "entity",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6427238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com