sql >> Database teknologi >  >> RDS >> Oracle

ORACLE SQL:Udfyld manglende datoer

Du kan få 30-dages perioden fra SYSDATE som følger (jeg antager, at du vil inkludere SYSDATE ?):

WITH mydates AS (
    SELECT TRUNC(SYSDATE) - 1 + LEVEL AS due_date FROM dual
   CONNECT BY LEVEL <= 31
)

Brug derefter ovenstående til at lave en LEFT JOIN med din forespørgsel (måske ikke en dårlig idé også at placere din forespørgsel i en CTE):

WITH mydates AS (
    SELECT TRUNC(SYSDATE) - 1 + LEVEL AS due_date FROM dual
   CONNECT BY LEVEL <= 31
), myorders AS (
    select 
    (case when trunc(so.revised_due_date) <= trunc(sysdate) 
        then trunc(sysdate) else trunc(so.revised_due_date) end) due_date, 
    (case 
        when (case when sp.pr_typ in ('VV','VD') then 'DVD' when sp.pr_typ in ('RD','CD') 
        then 'CD' end) = 'CD' 
        and  (case when so.tec_criteria in ('PI','MC') 
        then 'XX' else so.tec_criteria end) = 'OF'
        then sum(so.revised_qty_due)
    end) CD_OF_VOLUME
    from shop_order so
    left join scm_prodtyp sp
    on so.prodtyp = sp.prodtyp
    where so.order_type = 'MD' 
    and so.plant = 'W' 
    and so.status_code between '4' and '8' 
    and trunc(so.revised_due_date) <= trunc(sysdate)+30
    group by trunc(so.revised_due_date), so.tec_criteria, sp.pr_typ
    order by trunc(so.revised_due_date)
)
SELECT mydates.due_date, myorders.cd_of_volume
  FROM mydates LEFT JOIN myorders
    ON mydates.due_date = myorders.due_date;

Hvis du vil vise et nul på "manglende" datoer i stedet for en NULL , brug COALESCE(myorders.cd_of_volume, 0) AS cd_of_volume ovenfor.



  1. Indfangning af linkede serverfejl

  2. MySQL LOG2() Funktion – Returner base-2 logaritmen for en værdi

  3. SQL, hvordan sammenkædes resultater?

  4. Advarsel:mysqli_connect():(HY000/2002):Ingen sådan fil eller mappe