1

I am working in Oracle SQL, suppose i have a table like this which has start date and completion date for stages of a project.

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

I need to bring the output as the below table. for stage level output, need to compare the completion date and start date and for project level, need to check the last stage of a project (i.e stage 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

can anyone help me?

3
  • Does your Project No column really have nulls in many places? Commented Apr 13, 2021 at 6:07
  • Hello Tim, Project No does not have any NULL entries Commented Apr 13, 2021 at 6:08
  • So, a stage is on time when it is completed the same day it started? And a project is on time when the last stage in the table is on time (regardless of any previous stages), yes? How far have you got? Where are you stuck? Please show the query you have so far. Do you have problems getting output 1 too or is it only output 2 you are having difficulties with? Commented Apr 13, 2021 at 7:23

1 Answer 1

3

You could write 2 queries, {Q1} returning the values you need for "Output 1", and {2} giving you the values for "Output 2". Once you see that these queries produce the correct result, JOIN them together. Example see DBfiddle.

Query 1 ("stage level")

select 
  projectno, stages, startdate, completiondate 
, case
    when startdate = completiondate then 'on time'
    else 'delayed'
  end output_1
from projects;

Query 2 ("project level")

-- 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
;

JOIN

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

ADDENDUM

( Taking @Thorsten Kettner's suggestion on board: ) You could also use max() in form of an analytic function eg

-- 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;

DBfiddle

Sign up to request clarification or add additional context in comments.

1 Comment

It would be more typical to use a mere window function (MAX OVER) instead of joining, but the join works, too, of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.