Oracle
 sql >> Baza danych >  >> RDS >> Oracle

Jak scalić moje zapytania w jedno zapytanie (lub może być przechowywaną procedurą ..)

Możesz utworzyć więcej niż jedną wirtualną tabelę z CTE, oddzielając definicje CTE przecinkami. Ponadto CTE mogą odnosić się do innych CTE.

Zakładając ep jest taki sam we wszystkich tych zapytaniach, możesz zrobić coś takiego:

 with ep as
    (select emp_cd,
      emp_num,
      to_char(pay_dt,'yyyymm') as pay_month,
      max(code),
      max(bill) as bill,
      max(chrg)  as charge,
      sum( nvl(pay_1,0)) sum_pay1,
      sum(nvl(pay_2, 0)) sum_pay2,
      (chrg_mon*22)+ (chrg_week*5)+ chrg_day as days,
    from emp_payments
    where emp_cd in ('HP','2000')
    and code     in ('X','Y','Z')
    group by emp_cd,
      emp_num,
      to_char(pay_dt,'yyyymm'),
      code
    ),
 chrg_orig (<field names here>) as (
  select emp_cd,
    emp_num,
    pay_month,
    max(code),
    sum(bill)
    case when sum(days)=22 then sum(chrg) else  round((round(sum(chrg)/sum(days),4)*22),2) end as chrg_orig
  from ep
  where chrg <>0
  group by 
  emp_cd,
  emp_num,
  paymonth
),
rate_chrg (<field names here>) as (
  select a.emp_cd,a.emp_num,a.key,b.rate as rate_chrg from 
      (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,max(invc_dt) as invc_dt from ep
          where code in ('X','Y') and rate <> 0
          group by emp_cd,emp_num,to_char(invc_dt,'yyyymm')) a,

      (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,invc_dt,rate from ep
          where code in ('X','Y') and rate <> 0) b
  where a.emp_cd = b.emp_cd
  and a.emp_num = b.emp_num
  and a.key = b.key
  and a.invc_dt = b.invc_dt
  ),
bonus_chrg (<field names here>) as (
  select a.emp_cd,a.emp_num,a.key,b.rate as bonus_chrg from 
      (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,max(invc_dt) as invc_dt from ep
          where code in ('Z') and rate <> 0
          group by emp_cd,emp_num,to_char(invc_dt,'yyyymm')) a,

      (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,invc_dt,rate from ep
          where code in ('Z') and rate <> 0) b
  where a.emp_cd = b.emp_cd
  and a.emp_num = b.emp_num
  and a.key = b.key
  and a.invc_dt = b.invc_dt
  ),
comp_days (<field names here>) as (
  select emp_cd,emp_num,paymonth as key,sum(days) as comp_days from ep
  where code in ('X','Y')
  group by emp_cd,emp_num,key
  )
SELECT *
FROM ep
LEFT OUTER JOIN chrg_orig
  ON <JOIN CONDITION>
LEFT OUTER JOIN rate_chrg
  ON <JOIN CONDITION>
LEFT OUTER JOIN bonus_chrg
  ON <JOIN CONDITION>
LEFT OUTER JOIN comp_days
  ON <JOIN CONDITION>



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Znajdź zakresy z serii liczb w SQL/Oracle

  2. Oracle generuje wiersze harmonogramu z interwałem

  3. Wstawiaj wiersze (nadrzędne i podrzędne) programowo

  4. JSON_QUERY() Funkcja w Oracle

  5. Wypełnij wiersze w kolumnie A wartością z kolumny B, jeśli warunek w kolumnie A jest spełniony