Query to select values greater than, where AS clause is used

10-11  Source: Network gathering  Views:0 

Advertisement
Oracle 10g
Requesting your help in writing the query correctly.
I have the following 2 tables. The AddProjectPhase and AddProject tables. It is many to many relationship. Each project can have a predefined set of 4 different phases under it. Each phase has a start date and an end date. I am writing a report to get the phases which have a duration greater than, for example, 3 months.
The query below is working fine to just display the list of all the phases along with the duration of each phase. I am not able to modify it to select the phases which have a duration, for example, 3 or more months.
CREATE TABLE  "ADDPROJECT"
   (     "VERSIONNO" NUMBER(*,0),
     "PROJID" VARCHAR2(20),
     "PROJNAME" VARCHAR2(60),
     "PROJSTARTDATE" DATE,
     "PROJSTATUS" VARCHAR2(20),
     "PROJENDDATE" DATE,
     "PROJENDTYPE" VARCHAR2(20),
     "PROJENDREASON" VARCHAR2(1000),
     "UCPROJECTMANAGER" VARCHAR2(20),
     "FROMDATE" DATE,
     "TODATE" DATE,
     "SRCHFIELD" VARCHAR2(20),
     "OPERATOR" VARCHAR2(20),
     "PARENTPROJID" VARCHAR2(20),
     "PROJHIDDENDATE" VARCHAR2(20),
      CONSTRAINT "PK_B36" PRIMARY KEY ("PROJID", "PROJHIDDENDATE") ENABLE
CREATE TABLE  "ADDPROJECTPHASE"
   (     "VERSIONNO" NUMBER(*,0),
     "PROJPHASEID" NUMBER(9,0),
     "PHASESTARTDATE" DATE,
     "PHASEENDDATE" DATE,
     "RRDATE" DATE,
     "PHASENAME" VARCHAR2(30),
     "PROJPHASESTATUS" VARCHAR2(20),
     "PROJID" VARCHAR2(20),
     "OPERATOR" VARCHAR2(20),
     "FROMDATE" DATE,
     "TODATE" DATE,
     "SRCHFIELD" VARCHAR2(20),
     "REVIEWCOMMENTS" VARCHAR2(1000),
     "PROJHIDDENDATE" VARCHAR2(20),
     "ISUEVALUATION" NUMBER(1,0),
     "SOLUTIONINGTEAMINVOLVEMENT" NUMBER(1,0),
     "ISUNAME" VARCHAR2(20),
      CONSTRAINT "PK_A63" PRIMARY KEY ("PROJPHASEID") ENABLE
   )Below is the query to display the list of all the phases along with the duration of each phase which is working fine.
SELECT pp.phaseName "phasename",
pp.phaseStartDate "phaseStartDate",
pp.phaseEndDate "phaseEndDate",
pp.projPhaseStatus "projPhaseStatus",
ap.projID "projID",
ap.projName "projName",
ap.projHiddenDate "projHiddenDate",
ap.projStartDate "projStartDate",
CASE
    WHEN pp.phaseEndDate IS NOT NULL
    THEN MONTHS_BETWEEN(1+pp.phaseEndDate,pp.phaseStartDate)
    WHEN pp.phaseEndDate IS NULL
    THEN MONTHS_BETWEEN(1+sysDate,pp.phaseStartDate)
    ELSE null
END "phaseMonths"
FROM AddProjectPhase pp, AddProject ap
WHERE ap.projID = pp.projID
AND ap.projHiddenDate = pp.projHiddenDate
ORDER BY ap.projIDHowever the modified query shown below to select all the phases greater than, for example, 3 months, is resulting in
ORA-00904: "PHASEMONTHS": invalid identifier SELECT pp.phaseName, pp.phaseStartDate, pp.phaseEndDate
FROM AddProjectPhase pp, AddProject ap
WHERE ap.projID = pp.projID
AND ap.projHiddenDate = pp.projHiddenDate
AND PhaseMonths IN
(SELECT
(CASE
WHEN pp.phaseEndDate IS NOT NULL
THEN MONTHS_BETWEEN(1+pp.phaseEndDate,pp.phaseStartDate)
WHEN pp.phaseEndDate IS NULL
THEN MONTHS_BETWEEN(1+sysDate,pp.phaseStartDate)
ELSE null
END) AS PhaseMonths
FROM AddProjectPhase pp, AddProject ap
WHERE ap.projID = pp.projID
AND ap.projHiddenDate = pp.projHiddenDate)
ORDER BY ap.projID
Looking for this?
select *
   from (
        SELECT pp.phaseName "phasename"
          , pp.phaseStartDate "phaseStartDate"
          , pp.phaseEndDate "phaseEndDate"
          , pp.projPhaseStatus "projPhaseStatus"
          , ap.projID "projID"
          , ap.projName "projName"
          , ap.projHiddenDate "projHiddenDate"
          , ap.projStartDate "projStartDate"
          , CASE WHEN pp.phaseEndDate IS NOT NULL THEN MONTHS_BETWEEN(1+pp.phaseEndDate,pp.phaseStartDate)
                 WHEN pp.phaseEndDate IS NULL     THEN MONTHS_BETWEEN(1+sysDate,pp.phaseStartDate)
                 ELSE null
            END "phaseMonths"
          FROM AddProjectPhase pp, AddProject ap
         WHERE ap.projID = pp.projID
           AND ap.projHiddenDate = pp.projHiddenDate
         ORDER
            BY ap.projID
  where "phaseMonths" >= 3
Related articles