[英]Comparing multiple rows and column in oracle
我在 Oracle SQL 工作,假設我有一個像這樣的表格,其中包含項目各個階段的開始日期和完成日期。
項目編號 | 階段 | 開始日期 | 完成日期 |
---|---|---|---|
PROJ_001 | 1 | 21 年 3 月 12 日 | 21 年 3 月 12 日 |
PROJ_001 | 2 | 21 年 3 月 14 日 | 21 年 3 月 14 日 |
PROJ_001 | 3 | 21 年 3 月 15 日 | 21 年 3 月 15 日 |
PROJ_001 | 4 | 21 年 3 月 18 日 | 21 年 3 月 18 日 |
PROJ_002 | 1 | 21 年 3 月 16 日 | 21 年 3 月 18 日 |
PROJ_002 | 2 | 21 年 3 月 17 日 | 21 年 3 月 19 日 |
PROJ_002 | 3 | 21 年 3 月 19 日 | 21 年 3 月 19 日 |
PROJ_002 | 4 | 21-MAR-21 | 21 年 3 月 23 日 |
我需要將 output 帶入下表。 對於階段級別output,需要比較完成日期和開始日期,對於項目級別,需要檢查項目的最后階段(即階段4)
項目編號 | 階段 | 開始日期 | 完成日期 | Output 1 | output 2項目級別 |
---|---|---|---|---|---|
PROJ_001 | 1 | 21 年 3 月 12 日 | 21 年 3 月 12 日 | 准時 | 准時 |
PROJ_001 | 2 | 21 年 3 月 14 日 | 21 年 3 月 14 日 | 准時 | 准時 |
PROJ_001 | 3 | 21 年 3 月 15 日 | 21 年 3 月 15 日 | 准時 | 准時 |
PROJ_001 | 4 | 21 年 3 月 18 日 | 21 年 3 月 18 日 | 准時 | 准時 |
PROJ_002 | 1 | 21 年 3 月 16 日 | 21 年 3 月 18 日 | 延遲 | 延遲 |
PROJ_002 | 2 | 21 年 3 月 17 日 | 21 年 3 月 19 日 | 延遲 | 延遲 |
PROJ_002 | 3 | 21 年 3 月 19 日 | 21 年 3 月 19 日 | 准時 | 延遲 |
PROJ_002 | 4 | 21-MAR-21 | 21 年 3 月 23 日 | 延遲 | 延遲 |
誰能幫我?
您可以編寫 2 個查詢,{Q1} 返回“輸出 1”所需的值,{2} 為您提供“輸出 2”的值。 一旦你看到這些查詢產生了正確的結果,就將它們連接在一起。 示例參見DBfiddle 。
查詢 1(“階段級別”)
select
projectno, stages, startdate, completiondate
, case
when startdate = completiondate then 'on time'
else 'delayed'
end output_1
from projects;
查詢 2(“項目級別”)
-- look at the last stage (only). CASE may need tweaking
select
projectno
, case
when max( startdate ) = max( completiondate ) then 'on time'
else 'delayed'
end output_2
from projects
group by projectno
;
加入
select Q1.*, Q2.output_2
from (
select
projectno, stages, startdate, completiondate
, case
when startdate = completiondate then 'on time'
else 'delayed'
end output_1
from projects
) Q1 join (
select
projectno
, case
when max( startdate ) = max( completiondate ) then 'on time'
else 'delayed'
end output_2
from projects
group by projectno
) Q2 on Q1.projectno = Q2.projectno
order by Q1.projectno, Q1.startdate
;
-- result
PROJECTNO STAGES STARTDATE COMPLETIONDATE OUTPUT_1 OUTPUT_2
PROJ_001 1 12-MAR-21 12-MAR-21 on time on time
PROJ_001 2 14-MAR-21 14-MAR-21 on time on time
PROJ_001 3 15-MAR-21 15-MAR-21 on time on time
PROJ_001 4 18-MAR-21 18-MAR-21 on time on time
PROJ_002 1 16-MAR-21 18-MAR-21 delayed delayed
PROJ_002 2 17-MAR-21 19-MAR-21 delayed delayed
PROJ_002 3 19-MAR-21 19-MAR-21 on time delayed
PROJ_002 4 21-MAR-21 23-MAR-21 delayed delayed
附錄
(采納@Thorsten Kettner 的建議:)您也可以使用分析 function 形式的 max() 例如
-- remove the comments -> see the output of max(...) over (...)
select
projectno, stages, startdate, completiondate
, case
when startdate = completiondate then 'on time'
else 'delayed'
end output_1
, case
when
max( startdate ) over ( partition by projectno )
= max( completiondate ) over ( partition by projectno )
then 'on time'
else 'delayed'
end output_2
-- , max( startdate ) over ( partition by projectno ) maxstart_
-- , max( completiondate ) over ( partition by projectno ) maxcompletion_
from projects;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.