oracle比较多行多列
Comparing multiple rows and column in oracle
我在 Oracle SQL 工作,假设我有一个这样的 table,其中包含项目各个阶段的开始日期和完成日期。
Project No
STAGES
Start Date
completion date
PROJ_001
1
12-MAR-21
12-MAR-21
PROJ_001
2
14-MAR-21
14-MAR-21
PROJ_001
3
15-MAR-21
15-MAR-21
PROJ_001
4
18-MAR-21
18-MAR-21
PROJ_002
1
16-MAR-21
18-MAR-21
PROJ_002
2
17-MAR-21
19-MAR-21
PROJ_002
3
19-MAR-21
19-MAR-21
PROJ_002
4
21-MAR-21
23-MAR-21
我需要将输出结果如下table。
对于阶段级输出,需要比较完成日期和开始日期
对于项目级别,需要检查项目的最后阶段(即第 4 阶段)
Project No
STAGES
Start Date
completion date
Output 1
output 2 project level
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
谁能帮帮我?
您可以编写 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 的建议:)您还可以以分析函数的形式使用 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;
我在 Oracle SQL 工作,假设我有一个这样的 table,其中包含项目各个阶段的开始日期和完成日期。
Project No | STAGES | Start Date | completion date |
---|---|---|---|
PROJ_001 | 1 | 12-MAR-21 | 12-MAR-21 |
PROJ_001 | 2 | 14-MAR-21 | 14-MAR-21 |
PROJ_001 | 3 | 15-MAR-21 | 15-MAR-21 |
PROJ_001 | 4 | 18-MAR-21 | 18-MAR-21 |
PROJ_002 | 1 | 16-MAR-21 | 18-MAR-21 |
PROJ_002 | 2 | 17-MAR-21 | 19-MAR-21 |
PROJ_002 | 3 | 19-MAR-21 | 19-MAR-21 |
PROJ_002 | 4 | 21-MAR-21 | 23-MAR-21 |
我需要将输出结果如下table。 对于阶段级输出,需要比较完成日期和开始日期 对于项目级别,需要检查项目的最后阶段(即第 4 阶段)
Project No | STAGES | Start Date | completion date | Output 1 | output 2 project level |
---|---|---|---|---|---|
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 |
谁能帮帮我?
您可以编写 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 的建议:)您还可以以分析函数的形式使用 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;