gpt4 book ai didi

sql - Oracle sql 将派生表与联合连接

转载 作者:行者123 更新时间:2023-12-04 09:43:34 27 4
gpt4 key购买 nike

我有一个这样的查询:

select       product_code,
count(foo.container_id) as "quantity_of_containers",
max((trunc(foo.update_date - foo.creation_date))) as "max_days"
from product
inner join stock on stock.product_id = product.product_id
inner join (
select arch.container_id,
arch.creation_date,
arch.update_date
from arch
union all
select
container.container_id,
container.creation_date,
container.update_date
from container) foo on stock.container_id = foo.container_id
group by product_code
order by "max_days" desc

问题是这个查询有什么问题,当我运行它时,它似乎没问题,但经过进一步检查,看起来只有来自容器的记录存在,出于某种原因,拱形表中没有一条记录。是否有不同的方式来编写具有相同逻辑的查询?我需要从 arch 和 container 中获取记录,因为一个表是存档,另一个是当前表。联合两个单独的查询容器和拱门的方法也在工作,但我正在寻找更快的东西。
@编辑:
我发送了一些示例数据,以便更清楚地说明我的实际意思。
+---------------+----------+----------+
| product_code | quantity | max_days |
+---------------+----------+----------+
| 5999990024965 | 345 | 85 |
| 5999990027614 | 326 | 81 |
| 5999990023753 | 87 | 77 |
+---------------+----------+----------+

来自拱形表的数据,
+---------------+----------+----------+
| product_code | quantity | max_days |
+---------------+----------+----------+
| 5999990082415 | 11 | 84 |
| 5999990059615 | 2 | 58 |
| 5999990023470 | 1 | 41 |
+---------------+----------+----------+

来自容器表的数据。

但是,当我运行查询时,我已粘贴到此处,我只从容器表中获取记录,
是的 stock.container_id 真的匹配 foo.container_ids (拱门和容器)

最佳答案

这不是答案,但评论太长了。

运行以下查询会得到什么?

select *
from product
inner join stock on stock.product_id = product.product_id
inner join (
select arch.container_id,
arch.creation_date,
arch.update_date,
'arch' qry
from arch
union all
select
container.container_id,
container.creation_date,
container.update_date,
'container' qry
from container) foo on stock.container_id = foo.container_id
where foo.qry = 'arch';

关于sql - Oracle sql 将派生表与联合连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51361758/

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