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

SQL til automatisk at generere manglende datoer og pris fra umiddelbart forudgående dato for manglende dato i tabel

Jeg har brugt din_tabel som én tabel med outerjoin , som brugt last_value for at indsætte tidligere data, tag et kig på dette :)

SELECT last_value(m.data ignore nulls) over (order by n.mydate) data,
     n.mydate
FROM
     (SELECT DATA, mydate FROM your_table
     ) m,
     (SELECT TRUNC(SYSDATE, 'MM')-1+LEVEL mydate FROM dual CONNECT BY LEVEL <= 30
     )n
WHERE m.mydate(+) = n.mydate
ORDER BY n.mydate;

spil her

Du kan bruge lag() fungerer også , men det udfylder ikke data, hvis datomellemrummet er mere end én., det udfylder kun umiddelbare tidligere data,

nvl(m.data, lag(m.data)over(order by n.mydate))

-redigering -

for dine data:

SELECT n.mydate VALUE_DATE,
     last_value(m.STK_EXCH IGNORE NULLS) OVER (ORDER BY n.mydate) STK_EXCH,
     last_value(m.SECURITY IGNORE NULLS) OVER (ORDER BY n.mydate) SECURITY,
     last_value(m.mkt_price IGNORE NULLS) OVER (ORDER BY n.mydate) MKT_PRICE
FROM
     (SELECT VALUE_DATE, STK_EXCH, SECURITY, MKT_PRICE FROM MKT
     ) m,
     (SELECT TRUNC(SYSDATE, 'MM')-1+LEVEL mydate FROM dual CONNECT BY LEVEL <= 30
     )n
WHERE TRUNC(m.VALUE_DATE(+)) = TRUNC(n.mydate)
ORDER BY n.mydate;

eller lidt mere specifik:brug nedenfor:

SELECT full_date,
     NVL(stk_exch,last_value(stk_exch IGNORE NULLS)OVER(ORDER BY full_date))stk_exch,
     NVL(security,last_value(security IGNORE NULLS)OVER(ORDER BY full_date))security,
     NVL(mkt_price,last_value(mkt_price IGNORE NULLS)OVER(ORDER BY full_date))mkt_price
FROM
     (SELECT TRUNC(m.vd,'MM')-1+LEVEL FULL_DATE
     FROM
          (SELECT MIN(VALUE_DATE) vd FROM mkt
          WHERE TO_CHAR( value_date, 'MM/YYYY') = TO_CHAR(sysdate-12,'MM/YYYY') -- this line may vary to your requirement
          ) m
     CONNECT BY LEVEL <= TO_CHAR(LAST_DAY(M.VD), 'DD')
     )first_q,
     ( SELECT value_date, stk_exch, SECURITY, mkt_price FROM mkt
     )r
WHERE first_q.full_date = TRUNC(r.value_date(+))
ORDER BY full_date;

se violin her




  1. Sådan indstilles TimeStamp med Timezone i Laravel 5 Migrate

  2. gitlab runner:mysqld:Kan ikke læse dir af '/etc/mysql/conf.d/'

  3. hvorfor udføres 'create table' i sql script 3 gange, når der kun bruges 1 create statement?

  4. Algebraiske datatyper i Postgres