【问题标题】:sql query for find open用于查找打开的 sql 查询
【发布时间】:2020-04-19 09:30:52
【问题描述】:

给定这个数据集:

     Date      |  Itemcode  | Rec or Out | DocType  |  Qty     |
    -----------+------------+-----------------------+----------+
    01/01/2020 |  100011    | Rec        | GRN      | 100      |
    01/01/2020 |  100011    | Out        | FA       |  50      |
    01/01/2020 |  100011    | Out        | FA       |  10      |
    02/02/2020 |  100011    | Out        | FA       |  30      |
    02/02/2020 |  100011    | REC        | GRN      | 100      |

我需要结果,第 2 天的开盘股票和走势以及收盘股票'

OpeningStock = day 1st (all rec- all out)' 
FA           = day 2nd (all out)' 
GRN          = day 2nd (all rec)'
ClosingStock=  openingstock+GRN-Fa


 Itemcode | OpeningStock | FA | GRN | ClosingStock |
 ---------+--------------+----+-----+--------------+
 100011   |     40       | 30 | 100 | 110          |

【问题讨论】:

  • 标记您正在使用的 DBMS。

标签: sql select stock


【解决方案1】:

试试下面这个逻辑-

DEMO HERE

WITH CTE
AS(
    SELECT Itemcode,MIN(Date) MD, 
    (
        SELECT SUM(CASE WHEN DocType = 'GRN' THEN Qty ELSE Qty*-1 END) 
        FROM your_table 
        WHERE Itemcode = A.Itemcode
        AND Date = MIN(A.Date)
    ) OS
    FROM your_table A
    GROUP BY Itemcode
)

SELECT B.Itemcode,
MAX(A.OS) OpeningStock ,
SUM(CASE WHEN DocType = 'FA' THEN Qty ELSE 0 END) FA,
SUM(CASE WHEN DocType = 'GRN' THEN Qty ELSE 0 END) GRN,
MAX(A.OS) +
    SUM(CASE WHEN DocType = 'GRN' THEN Qty ELSE 0 END) -
    SUM(CASE WHEN DocType = 'FA' THEN Qty ELSE 0 END)
AS ClosingStock 
FROM CTE A
INNER JOIN your_table B
ON A.MD < B.Date AND A.Itemcode = B.Itemcode
GROUP BY B.Itemcode

【讨论】:

    【解决方案2】:

    您可以使用窗口功能:

    select itemcode, rec_qty - Out_qty as OpeningStock, FA, GRN, 
           ((rec_qty - Out_qty) + GRN - FA) as ClosingStock
    from(select itemcode, 
                sum(case when seq = 1 and rec = 'rec' then qty end) as rec_qty,
                sum(case when seq = 1 and rec = 'Out' then qty end) as Out_qty,
                sum(case when seq = 2 and rec = 'Out' then qty end) as FA,
                sum(case when seq = 2 and rec = 'rec' then qty end) as grn
         from(select t.*, dense_rank() over (partition by itemcode order by date) as seq
              from table t
             ) t
         group by itemcode
        ) t;
    

    【讨论】:

    • 嗨,感谢您的帮助,但在这里我提到日期,因为不同时期会找到开盘和收盘股票
    【解决方案3】:

    您只需要使用窗口函数提供的累积总和。您可以使用以下方法获取所有日期的值:

    select date, itemcode,
           sum(sum(case when rec = 'Rec' then qty else - qty end)) over
               (partition by itemcode
                order by date
                rows between unbounded preceding and 1 preceding
               ) as openingstock
           sum(case when doctype = 'FA' then qty end) as fa,
           sum(case when doctype = 'GRN' then qty end) as grn,
           sum(sum(case when rec = 'Rec' then qty else - qty end)) over
               (partition by itemcode order by date) as closingstock
    from t
    group by date, itemcode;
    

    如果要选择特定日期,可以将其用作子查询。

    【讨论】:

      猜你喜欢
      • 2016-05-28
      • 2017-01-14
      • 2011-07-29
      • 1970-01-01
      • 2021-02-09
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      相关资源
      最近更新 更多