我正在处理这个特别复杂的事情(至少对我而言,如果你是一个数学极客,你不评判我!)问题。
我基本上有两件事记录了当前的库存水平以及商店内外的所有库存变动清单。我正在尝试将这两部分结合起来,使我能够在去年的任何时候理解特定物体的库存水平。
SQL的第一部分结合了去年特定商店中所有对象和当前库存水平的所有库存变动:
SELECT OBJINCDE,
STOREINCDE,
TRUNC(STKMVTDTE) AS MOVEMENT_DATE,
--This CASE statement tells me if the stock was moved in or out
CASE WHEN STKMVTINCDE IN (1, 2, 3, 5, 6, 8, 9, 11) THEN 1 ELSE -1 END AS MOVEMENT
FROM H_STK
WHERE TRUNC(STKMVTDTE, 'MM') >= ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -12) --in the last year
AND STOREINCDE = 615 --A particular store
UNION ALL
--This statement looks at current levels and combines it with movements as a movement in on the date that the statement was run
SELECT OBJINCDE,
STOREINCDE,
TRUNC(SYSDATE) AS MOVEMENT_DATE,
STKQTY AS MOVEMENT
FROM P_OBJSTORE
WHERE (STKBRKQTY > 0 OR STKMAXQTY > 0) --This just only picks objects that have a maximum or minimum listed don't judge the stock system either, I can't change that
AND STOREINCDE = 615
因此,基本上返回所有股票走势及其日期的清单,然后我将在此声明中使用:
SELECT TO_CHAR(y.EACH_DAY, 'DD/MM/YYYY') AS EACH_DAY,
x.OBJINCDE AS OBJINCDE,
NVL(x.MOVEMENT, 0) AS MOVEMENT,
SUM(NVL(x.MOVEMENT, 0)) OVER ( ORDER BY y.EACH_DAY DESC) AS STOCK_LEVEL --Oracle analytics to put together a running total
FROM (SELECT OBJINCDE, MOVEMENT_DATE, SUM(MOVEMENT) AS MOVEMENT
FROM W_MIN_MAX_MOVEMENTS
WHERE OBJINCDE = 14419 --This is my selection of a particular object
GROUP BY OBJINCDE, MOVEMENT_DATE
HAVING SUM(MOVEMENT) <> 0
ORDER BY MOVEMENT_DATE) x,
(SELECT TRUNC(SYSDATE) - 365 + LEVEL AS EACH_DAY --Just brings in each day for the last 365 days
FROM DUAL
WHERE ROWNUM <= 365
CONNECT BY LEVEL = ROWNUM) y
WHERE x.MOVEMENT_DATE (+) = y.EACH_DAY
ORDER BY y.EACH_DAY DESC
所以在那之后我有一些问题似乎无法解决。
首先 - 在第二个语句中,它返回365天的列表,当天所选对象的移动及其历史库存水平,我似乎无法让对象ID出现在每一行中。
第二 - 我希望能够运行这个以便我为每个物体进行365天的移动,并且当天的相应库存水平。我认为这必须包括对Oracle Analytics的更好理解,而不是我目前所拥有的。
非常感谢任何帮助。
答案 0 :(得分:1)
你需要一个对象列表,我会在stock视图中使用一个不同的但你可能有更好的东西,可能是另一个父表:
SELECT to_char(cal.each_day, 'dd/mm/yyyy') AS each_day,
obj.objincde AS objincde,
nvl(sto.movement, 0) AS movement,
SUM(nvl(sto.movement, 0)) over(ORDER BY cal.each_day DESC)
AS stock_level --oracle analytics to put together a running total
FROM (SELECT DISTINCT objincde
FROM w_min_max_movements
WHERE objincde = 14419 --this is my selection of a particular object
) obj
CROSS JOIN (SELECT trunc(SYSDATE) - 365 + LEVEL
AS each_day --just brings in each day for the last 365 days
FROM dual
WHERE rownum <= 365
CONNECT BY LEVEL = rownum) cal
LEFT JOIN (SELECT objincde, movement_date, SUM(movement) AS movement
FROM w_min_max_movements
GROUP BY objincde, movement_date
HAVING SUM(movement) <> 0) sto
ON sto.movement_date = cal.each_day
AND sto.objincde = obj.objincde
ORDER BY cal.each_day DESC
这将使您的对象ID出现在每一行上。
顺便说一句,从现在开始你应该尝试使用ANSI连接:旧的连接更难阅读,有额外的限制,只支持遗留代码。所有新代码都应使用ANSI连接。
现在,如果要显示所有对象的所有日期,只需删除WHERE
子句并在分析函数中添加PARTITION
子句:
SELECT to_char(cal.each_day, 'dd/mm/yyyy') AS each_day,
obj.objincde AS objincde,
nvl(sto.movement, 0) AS movement,
SUM(nvl(sto.movement, 0))
over(PARTITION BY obj.objincde ORDER BY cal.each_day DESC)
AS stock_level --oracle analytics to put together a running total
FROM (SELECT DISTINCT objincde
FROM w_min_max_movements) obj
CROSS JOIN (SELECT trunc(SYSDATE) - 365 + LEVEL
AS each_day --just brings in each day for the last 365 days
FROM dual
WHERE rownum <= 365
CONNECT BY LEVEL = rownum) cal
LEFT JOIN (SELECT objincde, movement_date, SUM(movement) AS movement
FROM w_min_max_movements
GROUP BY objincde, movement_date
HAVING SUM(movement) <> 0) sto
ON sto.movement_date = cal.each_day
AND sto.objincde = obj.objincde
ORDER BY cal.each_day DESC