如何确保CASE语句的结果不会显示在我的查询中?

时间:2014-10-01 21:17:09

标签: sql oracle case alias

我正在开发Oracle中的评估项目,并想知道如何确保CASE语句的结果不会显示在我的查询中。我尝试过使用ALIAS名称,但它不喜欢它,因为它是一个“无效的标识符”。有人告诉我你不能在'Where'子句中使用ALIAS名称?不确定这是否正确。

我确信这很简单,但我找不到问题。这是我到目前为止所做的工作:

--QUERY 1--
SELECT Department 
       , Full_Name_Supervisor
       , Supervisor_Email
       , Full_Name, Email_Address
       , Start_Date
       , Classification
       , Job_Title
       , State_or_Local_Fund
    --   ,(extract(DAY from Start_Date)||'/'||extract(MONTH from Start_Date)||'/'||extract(YEAR from sysdate))
--START OF CASE STATEMENTS--
,(CASE WHEN Classification = 'CS' THEN
      (CASE WHEN add_months(Start_Date, 3) BETWEEN trunc(Sysdate - 30) AND trunc(Sysdate - 1) THEN '3M 30 Days Past'
             WHEN add_months(Start_Date, 3) BETWEEN trunc(Sysdate) AND trunc(Sysdate + 89) THEN '3M Next 90 Days'
             WHEN add_months(Start_Date, 6) BETWEEN trunc(Sysdate - 30) AND trunc(Sysdate - 1) THEN '6M 30 Days Past'
             WHEN add_months(Start_Date, 6) BETWEEN trunc(Sysdate) AND trunc(Sysdate + 89) THEN '6M Next 90 Days'
             WHEN add_months(Start_Date, 12) BETWEEN trunc(Sysdate - 30) AND trunc(Sysdate - 1) THEN '12M 30 Days Past'
             WHEN add_months(Start_Date, 12) BETWEEN trunc(Sysdate) AND trunc(Sysdate + 89) THEN '12M Next 90 Days'
             WHEN to_date(decode(extract(DAY from Start_Date)||'/'||extract(MONTH from Start_Date),'29/2','28/2',extract(DAY from Start_Date)||'/'||extract(MONTH from Start_Date))||'/'||extract(YEAR from sysdate),'dd/mm/yyyy') BETWEEN trunc(Sysdate - 30) AND trunc(Sysdate - 1) THEN 'Annual 30 Days Past'
             WHEN to_date(decode(extract(DAY from Start_Date)||'/'||extract(MONTH from Start_Date),'29/2','28/2',extract(DAY from Start_Date)||'/'||extract(MONTH from Start_Date))||'/'||extract(YEAR from sysdate),'dd/mm/yyyy') BETWEEN trunc(Sysdate) AND trunc(Sysdate + 89) THEN 'Annual Next 90 Days'

      END)
  WHEN Classification = 'AP' THEN
        (CASE  WHEN to_date('01-JAN'||(EXTRACT(YEAR FROM sysdate)+ 1),'DD-MON-YYYY') BETWEEN trunc(sysdate - 30) AND trunc(sysdate - 1) THEN 'Annual 30 Days Past'
               WHEN to_date('01-JAN'||(EXTRACT(YEAR FROM sysdate)+ 1),'DD-MON-YYYY') BETWEEN trunc(Sysdate) AND trunc(Sysdate + 89) THEN 'Annual Next 90 Days'      
        END)
  WHEN Classification = 'ST' THEN
       (CASE WHEN to_date('01-MAY'||(EXTRACT(YEAR FROM sysdate)), 'DD-MON-YYYY') BETWEEN trunc(sysdate - 30) AND trunc(sysdate - 1) THEN 'Semester 30 Days Past'             
             WHEN to_date('01-MAY'||(EXTRACT(YEAR FROM sysdate)), 'DD-MON-YYYY') BETWEEN trunc(Sysdate) AND trunc(Sysdate + 89) THEN 'Semester Next 90 Days' 
             WHEN to_date('01-AUG'||(EXTRACT(YEAR FROM sysdate)), 'DD-MON-YYYY') BETWEEN trunc(sysdate - 30) AND trunc(sysdate - 1) THEN 'Semester 30 Days Past'             
             WHEN to_date('01-AUG'||(EXTRACT(YEAR FROM sysdate)), 'DD-MON-YYYY') BETWEEN trunc(Sysdate) AND trunc(Sysdate + 89) THEN 'Semester Next 90 Days' 
             WHEN to_date('01-DEC'||(EXTRACT(YEAR FROM sysdate)), 'DD-MON-YYYY') BETWEEN trunc(sysdate - 30) AND trunc(sysdate - 1) THEN 'Semester 30 Days Past'             
             WHEN to_date('01-DEC'||(EXTRACT(YEAR FROM sysdate)), 'DD-MON-YYYY') BETWEEN trunc(Sysdate) AND trunc(Sysdate + 89) THEN 'Semester Next 90 Days' 
        END)
END) AS Evaluation_Due  --This is the last column which only Non-Null values should be shown.

FROM
--Query 2--
(SELECT DISTINCT
   (SELECT Min(q2.effective_start_date)
        FROM hr.per_all_assignments_f q2
            WHERE q2.assignment_id = paaf.assignment_id
                  AND 'Evaluation_Due' > ' '
                 AND q2.job_id = paaf.job_id) AS Start_Date 
   ,papf.email_address          Email_Address
   ,papf.full_name              Full_Name
   ,haou.name                   Department
   ,pjd.segment6                Classification
   ,pjd.segment9                Job_Title
   ,ppapf.full_name             Full_Name_Supervisor
   ,ppapf.email_address         Supervisor_Email
   ,(CASE when substr(pcak.segment1, 2,3) = '810' then 'State'
          when substr(pcak.segment1, 2,3) = '010' then 'State'
              ELSE 'Local'
END) State_or_Local_Fund
FROM hr.per_all_people_f papf,
     hr.per_all_assignments_f paaf,
     hr.per_all_people_f ppapf,
     hr.hr_all_organization_units haou,
     hr.per_jobs pj,
     hr.per_job_definitions pjd,
     hr.pay_cost_allocations_f pcaf,
     hr.pay_cost_allocation_keyflex pcak
--Joining fields--
WHERE paaf.person_id = papf.person_id
AND papf.person_id = paaf.person_id
AND paaf.organization_id = haou.organization_id
AND pcak.cost_allocation_keyflex_id = pcaf.cost_allocation_keyflex_id
AND pcaf.assignment_id = paaf.assignment_id
AND pj.job_definition_id = pjd.job_definition_id
AND paaf.job_id = pj.job_id    
AND ppapf.person_id (+) = paaf.supervisor_id
-- Truncating sysdate--
AND trunc(sysdate) BETWEEN papf.effective_start_date and papf.effective_end_date
AND trunc(sysdate) BETWEEN paaf.effective_start_date and paaf.effective_end_date
AND trunc(sysdate) BETWEEN pcaf.effective_start_date and pcaf.effective_end_date
AND trunc(sysdate) BETWEEN ppapf.effective_start_date (+) and ppapf.effective_end_date (+)
--Constraints--
AND (pjd.segment3 != 'E'
AND pjd.segment9 != 'Retiree'
AND pjd.segment6 IN ('CS','AP','ST')
AND paaf.assignment_status_type_id = 1

))

如果需要更多信息,请与我们联系。谢谢!

0 个答案:

没有答案