gpt4 book ai didi

sql - 在另一个语句中使用语句的结果

转载 作者:行者123 更新时间:2023-12-04 02:44:37 25 4
gpt4 key购买 nike

我有以下 2 个 SQL 语句

select 
Fleet, SUM(hours) as Operating
from
table
where
colE = 'Operating'
and START_TIME >= '2012-01-01' and END_TIME<='2013-01-01'
and FLEET IS NOT NULL
group by
fleet
order by
FLEET asc


select Fleet, SUM(hours) as Delay
from table
where colE='Delay' and START_TIME>='2012-01-01' and END_TIME<='2013-01-01' and FLEET IS NOT NULL
group by fleet order by FLEET asc

我需要这两个语句的结果来基本显示

       select Fleet, (Operating / Delay ) as Calculated Col from table

任何人都可以帮助我引导我朝着如何做到这一点的方向前进吗?刚接触 sql,所以我认为我应该使用临时表?

谢谢!

最佳答案

简单的 INNER JOIN 应该这样做:

SELECT op.Fleet, (Operating / Delay) AS Calc FROM
(select
Fleet, SUM(hours) as Operating
from table
where
colE = 'Operating'
and START_TIME >= '2012-01-01' and END_TIME<='2013-01-01'
and FLEET IS NOT NULL
group by fleet
) AS op INNER JOIN (
select Fleet, SUM(hours) as Delay
from table
where colE='Delay' and START_TIME>='2012-01-01'
and END_TIME<='2013-01-01' and FLEET IS NOT NULL
group by fleet
) AS de ON op.Fleet = de.Fleet
ORDER BY op.Fleet ASC

关于sql - 在另一个语句中使用语句的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19096768/

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