2017-03-20 92 views
0

我正在使用以下腳本來獲取特定製造訂單的訂單歷史記錄;Oracle SQL,根據訂單歷史記錄計算下一訂單數量

select ds.status, ds.catnr, ds.part_no, ds.print_type, ds.nr_discs, ds.qty, ds.ship_date 
from 
(select 'Open Order' status, gb.catnr, gb.part_no, decode(gb.tec_criteria,'XX','SCREEN','OF','OFFSET','PI','OFFSET','MC','OFFSET') print_type, sp.nrunits nr_discs, sum(gb.or_menge_fd) qty, min(trunc(gb.shd_date)) ship_date 
from gps_beweg gb, oes_customer oc, scm_packtyp sp 
where gb.part_no = 'A0101628358-VV92-1900' 
and gb.uebergabe_oes = '1' 
and gb.pwerk_disc = 'W' 
and gb.cunr = oc.cunr 
and gb.packtyp = sp.packtyp 
group by gb.cunr, oc.name, gb.part_no, sp.nrunits, gb.tec_criteria, gb.catnr, gb.prodtyp, gb.packtyp 
UNION ALL 
select unique 'Shipped Order' status, 
null catnr, null part_no, null print_type, null nr_discs, 
(select sum(ds1.planqty) from oes_delsegview ds1 where ds.ordnr = ds1.ordnr and ds.catnr = ds1.catnr and ds.prodtyp = ds1.prodtyp and ds.packtyp = ds1.packtyp) qty, 
(select trunc(max(ds1.gps_planshpdate)) from oes_delsegview ds1 where ds.ordnr = ds1.ordnr and ds.catnr = ds1.catnr and ds.prodtyp = ds1.prodtyp and ds.packtyp = ds1.packtyp) ship_date 
from part_description pd1, oes_delsegview ds 
where pd1.part_no = 
    (select max(gb.part_no) 
     from gps_beweg gb 
     where gb.part_no = 'A0101628358-VV92-1900' 
     and gb.uebergabe_oes = '1' 
     and gb.pwerk_disc = 'W') 
and pd1.catnr = ds.catnr 
and pd1.prodtyp = ds.prodtyp 
and pd1.packtyp = ds.packtyp 
and ds.ord_o_status in ('7','9') 
order by status, ship_date desc) ds 
where rownum <=5 

此腳本的結果看起來是這樣的......

enter image description here

我想用數據的數量和SHIP_DATE列來預測下一個數量和日期。我可以使用TREND函數在Excel中執行此操作。有沒有辦法在SQL中做到這一點?它是否符合REGR_SLOPE函數(我似乎無法讓我的頭瞭解它如何工作!?!)。

+0

據我所知,在Oracle的SQL中沒有內容可以幫助你。儘管存在預測報告功能:https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_commands_1052.htm#OLADM822。我從來沒有使用過,實際上也不知道如何調用它。您可以以某種方式使用它,或者使用Excel或任何提供該功能的工具在DBMS之外執行此操作。 –

+0

感謝您的反饋,我將檢查預測功能(感謝您的鏈接)。我想我可能不得不恢復使用Excel功能。 – SMORF

回答

1

如前所述,據我所知,Oracle的SQL沒有內置的趨勢函數來幫助你。但是,你可以做的就是利用分析功能並提出一些算法。

ship_date - LAG(ship_date) OVER (ORDER BY ship_date)爲您提供最後訂單和當前訂單之間的日期。你必須加權這些值,但是,請將它們乘以ROW_NUMBER() OVER (ORDER BY ship_date)。然後除以得到值加到MAX(ship_date)

這裏是相應的查詢。有點難以閱讀和理解,但在我看來仍然是一種選擇。該查詢將檢索您的所有行以及每行的趨勢日期和數量。所以你可以看到什麼是在某個時間預測的,什麼是真正的貨物。最後一行給出了當前的預測。

select 
    status, catnr, part_no, print_type, nr_discs, qty, ship_date, 
    round(qty + sum(qty_diff_weighted) over (order by rn)/
    (sum(rn) over (order by rn) - 1)) as trend_qty, 
    round(ship_date + sum(date_diff_weighted) over (order by rn)/
    (sum(rn) over (order by rn) - 1)) as trend_date 
from 
(
    select 
    status, catnr, part_no, print_type, nr_discs, qty, ship_date, 
    row_number() over (order by ship_date) * 
     (qty - lag(qty) over (order by ship_date)) as qty_diff_weighted, 
    row_number() over (order by ship_date) * 
     (ship_date - lag(ship_date) over (order by ship_date)) as date_diff_weighted, 
    row_number() over (order by ship_date) as rn 
    from (your query) 
) 
order by ship_date; 

結果:

 
STATUS   CATNR ... QTY SHIP_DATE TREND_QTY TREND_DATE 
Shipped Order     500 06.06.2014 
Shipped Order     500 17.11.2014 500   30.04.2015 
Shipped Order     300 21.09.2015 180   28.05.2016 
Shipped Order     300 16.08.2016 233   29.05.2017 
Open Order  PPD168  300 24.03.2017 257   11.12.2017 

這表明該技術。當然,你可以想出一個完全不同的算法,更適合你。

+0

這些結果與我從Excel趨勢中得到的結果並不遙遠......我可能會測試一些不同的算法,但我非常感謝這些建議。這無疑給我指出了正確的方向。謝謝。 – SMORF