Sql 如何通过关联这两个表来显示可用数量
我有两个表,tbl_storage_Depositor_Master和tbl_storage_Depositor_log,一个表的总数量为1000,另一个表的事务量为500300100 现在我想显示如下数据:Sql 如何通过关联这两个表来显示可用数量,sql,sql-server,Sql,Sql Server,我有两个表,tbl_storage_Depositor_Master和tbl_storage_Depositor_log,一个表的总数量为1000,另一个表的事务量为500300100 现在我想显示如下数据: Date issue quantity available quantity ----------------------------------------------------- 20/10/2014 500
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 ?
如何计算可用数量
请帮忙
根据答案,但有错误。一种方法是使用递归CTE
模式设置
质疑
结果
更新尝试这样更改您的查询
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
您好,我正在使用以下代码,但出现错误…Msg 240,级别16,状态1,行1类型在递归查询cte1的\u包的第\u列中的锚和递归部分之间不匹配。Msg 240,级别16,状态1,行1类型在递归查询cte1的rn列中的锚和递归部分之间不匹配。Msg 240,级别16,状态1,行1类型在递归查询cte1的CreatedDate列中的锚和递归部分之间不匹配。Msg 240,级别16,状态1,行1类型在列available_quantity中的锚定和递归部分之间不匹配o@user3373573-现在更新支票。你完全搞乱了ctethank现在只有一个错误消息240,级别16,状态1,行1类型在递归查询cte1的可用数量列中的锚和递归部分之间不匹配。
;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