1

我正在处理这个特别复杂的问题(至少对我来说,如果你是一个数学极客,不要评判我!)问题。

我基本上有两件事记录当前的库存水平,以及所有进出商店的库存变动清单。我试图将这两个部分结合起来,让我能够了解去年任何时候特定对象的库存水平。

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 分析的更好理解。

任何帮助将不胜感激。

4

1 回答 1

1

您需要一个对象列表,我将在股票视图上使用 distinct 但您可能有更好的东西,另一个父表可能:

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
于 2013-09-25T09:48:31.743 回答