返回指定日期前几行的总和

时间:2019-04-26 17:57:50

标签: sql

我正在开发一份报告,以显示与飞机定期维护任务有关的非常规事件。但是,为了进行统计分析(威布尔),我需要在完成计划的维护任务时返回飞机总时数和飞机总循环数的总和。

我需要加入两个表。

第一个(飞机维修任务完成)也被列为以下代码。 第二个(Aircraft_Actual_Flights)显示飞机执行的每个航班。

我已经尝试使用Over Partition By,但是它只返回了整个“飞行小时数”和“周期数”列的总和。

1º表格(飞机维修任务的完成情况)及其代码:

EO        AC    ORDER_CATEGORY  WO  ACCOMPLISHED_DATE
SVC-1   XX-XXX      WO        375983    07/04/2019
SVC-1   XX-XXX      WO        379384    08/04/2019
SVC-1   XX-XXX      WO        380459    21/04/2019
SVC-1   XX-XXX      WO        376196    25/04/2019
SELECT EO,
AC,
ORDER_CATEGORY,
ORDER_NO,
RESET_DATE AS "ACCOMPLISHED_DATE"

FROM ODB.ENGINEERING_COMPLIANCE

WHERE EO = 'A32X-05-00-00-SVC-1'

AND AC = 'XX-XXX'

AND RESET_DATE BETWEEN 07/04/2019 AND 08/04/2019

2º桌子(Aircraft_Actual_Flights)及其代码:

AC     FLIGH_DATE   FLIGHT_HOURS    CYCLES
XX-XXX  07/04/2019         2          1
XX-XXX  07/04/2019         2          1
XX-XXX  07/04/2019         3          1
XX-XXX  07/04/2019         1          1
XX-XXX  07/04/2019         1          1
XX-XXX  08/04/2019         6          1
XX-XXX  21/04/2019         5          1
XX-XXX  21/04/2019         2          1
XX-XXX  25/04/2019         1          1
SELECT AC,
FLIGHT_DATE,
FLIGHT_HOURS,
CYCLES

FROM ODB.AC_ACTUAL_FLIGHTS

AND AC = 'XX-XXX'

当我加入联接时,它仅显示总飞行小时数和总循环数,但它应该返回直到完成日期为止的飞行总和。

SELECT A.EO,
A.AC,
A.ORDER_CATEGORY,
A.ORDER_NO,
A.RESET_DATE AS "ACCOMPLISHED_DATE"
SUM(B.FLIGHT_HOURS) OVER(PARTITION BY B.AC) AS "TOTAL FH",
SUM(B.CYCLES) OVER(PARTITION BY B.AC) AS "TOTAL FC"

FROM ODB.ENGINEERING_COMPLIANCE A
LEFT JOIN ODB.AC_ACTUAL_FLIGHTS B
ON A.AC = B.AC

WHERE EO = 'A32X-05-00-00-SVC-1'

AND AC = 'XX-XXX'

AND RESET_DATE >= SYSDATE-7
EO  AC  ORDER_CATEGORY  WO  ACCOMPLISHED_DATE   TOTAL FH    TOTAL FC
SVC-1   XX-XXX  WO  375983  20/04/2019  23  9
SVC-1   XX-XXX  WO  379384  23/04/2019  23  9
SVC-1   XX-XXX  WO  380459  25/04/2019  23  9
SVC-1   XX-XXX  WO  376196  26/04/2019  23  9

请注意,23是飞机的总飞行小时,9是飞机XX-XXX的总飞行周期。它应该有一个从完成日期起对行数(飞行时间和飞行周期)求和的子句。与此类似:flight_date <= reset_date时的总和。

伙计们,您能帮我解决这个问题吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用公共表格表达式来获取不同的日期,然后将所有3个词加入选择中。

DECLARE @tbl1 TABLE(EO varchar(20), AC varchar(20), AccompDate datetime)
DECLARE @tbl2 TABLE(AC varchar(20), FlyDate datetime, FHours int, FCycles int)

INSERT INTO @tbl1 VALUES ('SVC-1', 'XX-XXX', '2019-04-07')
INSERT INTO @tbl1 VALUES ('SVC-1', 'XX-XXX', '2019-04-08')
INSERT INTO @tbl1 VALUES ('SVC-1', 'XX-XXX', '2019-04-21')
INSERT INTO @tbl1 VALUES ('SVC-1', 'XX-XXX', '2019-04-25')   

INSERT INTO @tbl2 VALUES ('XX-XXX', '2019-04-07', 2 ,1 )
INSERT INTO @tbl2 VALUES ('XX-XXX', '2019-04-07', 2 ,1 )
INSERT INTO @tbl2 VALUES ('XX-XXX', '2019-04-07', 3 ,1 )
INSERT INTO @tbl2 VALUES ('XX-XXX', '2019-04-07', 1 ,1 )
INSERT INTO @tbl2 VALUES ('XX-XXX', '2019-04-07', 1 ,1 )
INSERT INTO @tbl2 VALUES ('XX-XXX', '2019-04-08', 6 ,1 )
INSERT INTO @tbl2 VALUES ('XX-XXX', '2019-04-21', 5 ,1 )
INSERT INTO @tbl2 VALUES ('XX-XXX', '2019-04-21', 2 ,1 )
INSERT INTO @tbl2 VALUES ('XX-XXX', '2019-04-25', 1 ,1 )

;WITH cteDistintAccompDates(AC , AccompDate) AS
(
SELECT AC, AccompDate FROM @tbl1 GROUP BY AccompDate, AC
)
SELECT t1.AC, t1.AccompDate, SUM(t2.Fhours), SUM(t2.FCycles)
FROM @tbl1 t1 
INNER JOIN @tbl2 t2 ON t1.AC = t2.AC
INNER JOIN cteDistintAccompDates t3 ON t2.FlyDate = t3.AccompDate 
AND t1.AccompDate >= t2.FlyDate
GROUP BY t1.AC, t1.AccompDate

AC  AccompDate  Fhours FCycles
XX-XXX  2019-04-07  9   5
XX-XXX  2019-04-08  15  6
XX-XXX  2019-04-21  22  8
XX-XXX  2019-04-25  23  9