如何通过关联这两个表来显示可用数量

时间:2015-02-27 09:18:06

标签: sql sql-server

我有2个表,tbl_storage_Depositor_Mastertbl_storage_Depositor_log,其中一个表的总数量为1000,另一个表具有500,300,100等问题的交易。

现在我想显示这样的数据:

Date           issue quantity      available quantity
-----------------------------------------------------
20/10/2014          500                  500
21/10/2014          300                  200
21/10/2014          100                  100

我的查询是:

SELECT 
    mg.Godown_Name, [Bags_Weight],
    CONVERT(VARCHAR(10), ssd.[CreatedDate], 103) AS Date
FROM
    [tbl_storage_Depositor_log] AS sdl
INNER JOIN
    tbl_MetaData_GODOWN AS mg ON sdl.Godown_ID = mg.Godown_ID
INNER JOIN
    tbl_MetaData_STACK AS ms ON sdl.Stack_ID = ms.Stack_ID
WHERE 
    Depositor_Id = '232700'

返回发行数量,行李,货仓和日期

date            godown            qty      availableqty
-------------------------------------------------------
20/10/2014         k1             500           ?
21/10/2014         k2             300           ?

如何计算可用数量?

请帮忙。

根据答案但有错误..

1 个答案:

答案 0 :(得分:0)

一种方法是使用Recursive CTE

架构设置

CREATE TABLE #test
  (
     Dates          DATE,
     issue_quantity INT
  )

INSERT #test
VALUES ('10/20/2014',500 ),
       ('10/21/2014',300 ),
       ('10/21/2014',100 )

<强>查询

;WITH cte
     AS (SELECT *,
                Row_number()OVER(ORDER BY dates) rn
         FROM   #test a),
     cte1
     AS (SELECT TOP 1 rn,
                      dates,
                      issue_quantity,
                      issue_quantity AS available_quantity
         FROM   cte
         ORDER  BY rn
         UNION ALL
         SELECT a.rn,
                a.dates,
                a.issue_quantity,
                b.available_quantity - a.issue_quantity as available_quantity
         FROM   cte a
                INNER JOIN cte1 b
                        ON a.rn - 1 = b.rn)
SELECT dates,
       issue_quantity,
       available_quantity
FROM   cte1 

<强>结果

dates       issue_quantity  available_quantity
----------  --------------  ------------------
2014-10-20  500             500
2014-10-21  300             200
2014-10-21  100             100

更新尝试更改您的查询。

WITH cte(Godown_Name, Stack_Name, No_Of_Bags, Bags_Weight, CreatedDate, rn)
     AS (SELECT mg.Godown_Name,
                ms.Stack_Name,
                [No_Of_Bags],
                [Bags_Weight],
                ssd.CreatedDate,
                Row_number()
                  OVER(
                    ORDER BY ssd.[CreatedDate]) rn
         FROM   [tbl_Delivery_Stacking_Details_GatePass] AS ssd
                INNER JOIN tbl_MetaData_GODOWN AS mg
                        ON ssd.Godown_ID = mg.Godown_ID
                INNER JOIN tbl_MetaData_STACK AS ms
                        ON ssd.Stack_ID = ms.Stack_ID
         WHERE  Depositor_WR_Id = '232700'),
     cte1
     AS (SELECT TOP 1 Godown_Name,
                      Stack_Name,
                      No_Of_Bags,
                      Bags_Weight,
                      rn,
                      CreatedDate,
                      [Bags_Weight] AS available_quantity
         FROM   cte
         ORDER  BY rn
         UNION ALL
         SELECT a.Godown_Name,
                a.Stack_Name,
                a.No_Of_Bags,
                a.[Bags_Weight],
                a.rn,
                a.CreatedDate,
                b.available_quantity - a.[Bags_Weight] AS available_quantity
         FROM   cte a
                INNER JOIN cte1 b
                        ON a.rn - 1 = b.rn)
SELECT CreatedDate,
       available_quantity
FROM   cte1