C# 从具有相同ID的不同表中减去2列

C# 从具有相同ID的不同表中减去2列,c#,datagridview,sql-server-2012,C#,Datagridview,Sql Server 2012,我有两个表TblAddToInventory和tblwithdrawnfromninventory。两者都有ProductID和Quantity。提取时,存货自然应扣除项目数量,但仅扣除已提取的项目。例如: TblAddToInventory TblWithdrawnFromInventory 这样,当我连接两个表并扣除特定列时,我应该有一个DataGridView,使用C#和以下数据: ProductID | Quantity | Amount 1 2

我有两个表
TblAddToInventory
tblwithdrawnfromninventory
。两者都有
ProductID
Quantity
。提取时,存货自然应扣除项目数量,但仅扣除已提取的项目。例如:

TblAddToInventory

TblWithdrawnFromInventory

这样,当我连接两个表并扣除特定列时,我应该有一个
DataGridView
,使用C#和以下数据:

ProductID | Quantity | Amount 

1           2          2.00

2           1          1.00

3           2          2.00
我知道如何使用
SUM
JOIN
但我不知道如何创建一种语法,从具有相同ID的不同表中减去两列

我不知道这是否正确,但我想的是
SUM
all from
TblAddToInventory
使用
groupby
然后
SUM
全部从
tblwithdrawnfromninventory
使用
groupby
然后
TblAddToInventory
中减去
列,然后
TblWithdrawnFromInventory
使用
分组依据
。但我认为这不是个好主意。你能帮忙吗

多谢各位

我知道如何使用SUM和JOIN,但我只是不知道如何创建一个 使用从不同表中减去两列的语法 相同的ID

这是如何执行此操作的代码:

 SELECT inventory.ProductId, 
            inventory.Quantity - ISNULL(withdrawal.Quantity,0) AS Quantity, 
            inventory.Amount - ISNULL(withdrawal.Amount,0) AS Amount
        FROM (
            SELECT ProductId, SUM(Quantity) AS Quantity, SUM(Amount) AS Amount
            FROM TblAddToInventory
            GROUP BY ProductId
        ) AS inventory
        LEFT JOIN (
            SELECT ProductId, SUM(Quantity) AS Quantity, SUM(Amount) AS Amount
            FROM TblWithdrawnFromInventory
            GROUP BY ProductId
        ) AS withdrawal ON inventory.ProductId = withdrawal.ProductId
我知道如何使用SUM和JOIN,但我只是不知道如何创建一个 使用从不同表中减去两列的语法 相同的ID

这是如何执行此操作的代码:

 SELECT inventory.ProductId, 
            inventory.Quantity - ISNULL(withdrawal.Quantity,0) AS Quantity, 
            inventory.Amount - ISNULL(withdrawal.Amount,0) AS Amount
        FROM (
            SELECT ProductId, SUM(Quantity) AS Quantity, SUM(Amount) AS Amount
            FROM TblAddToInventory
            GROUP BY ProductId
        ) AS inventory
        LEFT JOIN (
            SELECT ProductId, SUM(Quantity) AS Quantity, SUM(Amount) AS Amount
            FROM TblWithdrawnFromInventory
            GROUP BY ProductId
        ) AS withdrawal ON inventory.ProductId = withdrawal.ProductId
准备: --使用数据创建临时表,强制转换第一行的日期以设置正确的数据类型

select * into #tblAddToInventory from (
select 1 as ProductID, 2 as Quantity, 2.00 as Amount, cast('7/7/2012' as date) as [Date]
union all select 2 as ProductID, 3 as Quantity, 3.00 as Amount, '7/7/2012' as Date 
union all select 3 as ProductID, 4 as Quantity, 4.00 as Amount, '7/7/2012' as Date 
union all select 2 as ProductID, 2 as Quantity, 2.00 as Amount, '7/8/2012' as Date 
union all select 3 as ProductID, 3 as Quantity, 3.00 as Amount, '7/8/2012' as Date 
) a 
select * into #tblWithdrawnFromInventory from (
select 2 as ProductID, 4 as Quantity, 4.00 as Amount, cast('7/9/2012' as date) as [Date]
union all select 3 as ProductID, 5 as Quantity, 5.00 as Amount, '7/10/2012' as Date 
) b 
select * from #tblAddToInventory union all 
select ProductID, (Quantity * -1) as Quantity, Amount, Date from #tblWithdrawnFromInventory

--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    1           2           2.00        2012-07-07
--    2           3           3.00        2012-07-07
--    3           4           4.00        2012-07-07
--    2           2           2.00        2012-07-08
--    3           3           3.00        2012-07-08
--    2           -4          4.00        2012-07-09
--    3           -5          5.00        2012-07-10

select ProductID, sum(Quantity) as Quantity, sum(Amount) as Amount from (
    select * from #tblAddToInventory union all 
    select ProductID, (Quantity * -1) as Quantity, (Amount * -1) as Amount, Date from #tblWithdrawnFromInventory
) joinedData
where [Date] >= '7/6/2012' and [Date] <= '7/11/2012'
group by ProductID

--    ProductID   Quantity    Amount
--    ----------- ----------- -----------
--    1           2           2.00
--    2           1           1.00
--    3           2           2.00
--使用数据创建临时表,强制转换第一行的日期以设置正确的数据类型

select * into #tblAddToInventory from (
select 1 as ProductID, 2 as Quantity, 2.00 as Amount, cast('7/7/2012' as date) as [Date]
union all select 2 as ProductID, 3 as Quantity, 3.00 as Amount, '7/7/2012' as Date 
union all select 3 as ProductID, 4 as Quantity, 4.00 as Amount, '7/7/2012' as Date 
union all select 2 as ProductID, 2 as Quantity, 2.00 as Amount, '7/8/2012' as Date 
union all select 3 as ProductID, 3 as Quantity, 3.00 as Amount, '7/8/2012' as Date 
) a 
select * into #tblWithdrawnFromInventory from (
select 2 as ProductID, 4 as Quantity, 4.00 as Amount, cast('7/9/2012' as date) as [Date]
union all select 3 as ProductID, 5 as Quantity, 5.00 as Amount, '7/10/2012' as Date 
) b 
select * from #tblAddToInventory union all 
select ProductID, (Quantity * -1) as Quantity, Amount, Date from #tblWithdrawnFromInventory

--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    1           2           2.00        2012-07-07
--    2           3           3.00        2012-07-07
--    3           4           4.00        2012-07-07
--    2           2           2.00        2012-07-08
--    3           3           3.00        2012-07-08
--    2           -4          4.00        2012-07-09
--    3           -5          5.00        2012-07-10

select ProductID, sum(Quantity) as Quantity, sum(Amount) as Amount from (
    select * from #tblAddToInventory union all 
    select ProductID, (Quantity * -1) as Quantity, (Amount * -1) as Amount, Date from #tblWithdrawnFromInventory
) joinedData
where [Date] >= '7/6/2012' and [Date] <= '7/11/2012'
group by ProductID

--    ProductID   Quantity    Amount
--    ----------- ----------- -----------
--    1           2           2.00
--    2           1           1.00
--    3           2           2.00
验证数据是否正确

select * from #tblAddToInventory
--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    1           2           2.00        2012-07-07
--    2           3           3.00        2012-07-07
--    3           4           4.00        2012-07-07
--    2           2           2.00        2012-07-08
--    3           3           3.00        2012-07-08
select * from #tblWithdrawnFromInventory
--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    2           4           4.00        2012-07-09
--    3           5           5.00        2012-07-10
验证数据是否正确

select * from #tblAddToInventory
--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    1           2           2.00        2012-07-07
--    2           3           3.00        2012-07-07
--    3           4           4.00        2012-07-07
--    2           2           2.00        2012-07-08
--    3           3           3.00        2012-07-08
select * from #tblWithdrawnFromInventory
--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    2           4           4.00        2012-07-09
--    3           5           5.00        2012-07-10
开始解决方案: --使用Union All连接查询,并将第二个查询乘以-1使其为负数

select * into #tblAddToInventory from (
select 1 as ProductID, 2 as Quantity, 2.00 as Amount, cast('7/7/2012' as date) as [Date]
union all select 2 as ProductID, 3 as Quantity, 3.00 as Amount, '7/7/2012' as Date 
union all select 3 as ProductID, 4 as Quantity, 4.00 as Amount, '7/7/2012' as Date 
union all select 2 as ProductID, 2 as Quantity, 2.00 as Amount, '7/8/2012' as Date 
union all select 3 as ProductID, 3 as Quantity, 3.00 as Amount, '7/8/2012' as Date 
) a 
select * into #tblWithdrawnFromInventory from (
select 2 as ProductID, 4 as Quantity, 4.00 as Amount, cast('7/9/2012' as date) as [Date]
union all select 3 as ProductID, 5 as Quantity, 5.00 as Amount, '7/10/2012' as Date 
) b 
select * from #tblAddToInventory union all 
select ProductID, (Quantity * -1) as Quantity, Amount, Date from #tblWithdrawnFromInventory

--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    1           2           2.00        2012-07-07
--    2           3           3.00        2012-07-07
--    3           4           4.00        2012-07-07
--    2           2           2.00        2012-07-08
--    3           3           3.00        2012-07-08
--    2           -4          4.00        2012-07-09
--    3           -5          5.00        2012-07-10

select ProductID, sum(Quantity) as Quantity, sum(Amount) as Amount from (
    select * from #tblAddToInventory union all 
    select ProductID, (Quantity * -1) as Quantity, (Amount * -1) as Amount, Date from #tblWithdrawnFromInventory
) joinedData
where [Date] >= '7/6/2012' and [Date] <= '7/11/2012'
group by ProductID

--    ProductID   Quantity    Amount
--    ----------- ----------- -----------
--    1           2           2.00
--    2           1           1.00
--    3           2           2.00
准备: --使用数据创建临时表,强制转换第一行的日期以设置正确的数据类型

select * into #tblAddToInventory from (
select 1 as ProductID, 2 as Quantity, 2.00 as Amount, cast('7/7/2012' as date) as [Date]
union all select 2 as ProductID, 3 as Quantity, 3.00 as Amount, '7/7/2012' as Date 
union all select 3 as ProductID, 4 as Quantity, 4.00 as Amount, '7/7/2012' as Date 
union all select 2 as ProductID, 2 as Quantity, 2.00 as Amount, '7/8/2012' as Date 
union all select 3 as ProductID, 3 as Quantity, 3.00 as Amount, '7/8/2012' as Date 
) a 
select * into #tblWithdrawnFromInventory from (
select 2 as ProductID, 4 as Quantity, 4.00 as Amount, cast('7/9/2012' as date) as [Date]
union all select 3 as ProductID, 5 as Quantity, 5.00 as Amount, '7/10/2012' as Date 
) b 
select * from #tblAddToInventory union all 
select ProductID, (Quantity * -1) as Quantity, Amount, Date from #tblWithdrawnFromInventory

--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    1           2           2.00        2012-07-07
--    2           3           3.00        2012-07-07
--    3           4           4.00        2012-07-07
--    2           2           2.00        2012-07-08
--    3           3           3.00        2012-07-08
--    2           -4          4.00        2012-07-09
--    3           -5          5.00        2012-07-10

select ProductID, sum(Quantity) as Quantity, sum(Amount) as Amount from (
    select * from #tblAddToInventory union all 
    select ProductID, (Quantity * -1) as Quantity, (Amount * -1) as Amount, Date from #tblWithdrawnFromInventory
) joinedData
where [Date] >= '7/6/2012' and [Date] <= '7/11/2012'
group by ProductID

--    ProductID   Quantity    Amount
--    ----------- ----------- -----------
--    1           2           2.00
--    2           1           1.00
--    3           2           2.00
--使用数据创建临时表,强制转换第一行的日期以设置正确的数据类型

select * into #tblAddToInventory from (
select 1 as ProductID, 2 as Quantity, 2.00 as Amount, cast('7/7/2012' as date) as [Date]
union all select 2 as ProductID, 3 as Quantity, 3.00 as Amount, '7/7/2012' as Date 
union all select 3 as ProductID, 4 as Quantity, 4.00 as Amount, '7/7/2012' as Date 
union all select 2 as ProductID, 2 as Quantity, 2.00 as Amount, '7/8/2012' as Date 
union all select 3 as ProductID, 3 as Quantity, 3.00 as Amount, '7/8/2012' as Date 
) a 
select * into #tblWithdrawnFromInventory from (
select 2 as ProductID, 4 as Quantity, 4.00 as Amount, cast('7/9/2012' as date) as [Date]
union all select 3 as ProductID, 5 as Quantity, 5.00 as Amount, '7/10/2012' as Date 
) b 
select * from #tblAddToInventory union all 
select ProductID, (Quantity * -1) as Quantity, Amount, Date from #tblWithdrawnFromInventory

--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    1           2           2.00        2012-07-07
--    2           3           3.00        2012-07-07
--    3           4           4.00        2012-07-07
--    2           2           2.00        2012-07-08
--    3           3           3.00        2012-07-08
--    2           -4          4.00        2012-07-09
--    3           -5          5.00        2012-07-10

select ProductID, sum(Quantity) as Quantity, sum(Amount) as Amount from (
    select * from #tblAddToInventory union all 
    select ProductID, (Quantity * -1) as Quantity, (Amount * -1) as Amount, Date from #tblWithdrawnFromInventory
) joinedData
where [Date] >= '7/6/2012' and [Date] <= '7/11/2012'
group by ProductID

--    ProductID   Quantity    Amount
--    ----------- ----------- -----------
--    1           2           2.00
--    2           1           1.00
--    3           2           2.00
验证数据是否正确

select * from #tblAddToInventory
--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    1           2           2.00        2012-07-07
--    2           3           3.00        2012-07-07
--    3           4           4.00        2012-07-07
--    2           2           2.00        2012-07-08
--    3           3           3.00        2012-07-08
select * from #tblWithdrawnFromInventory
--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    2           4           4.00        2012-07-09
--    3           5           5.00        2012-07-10
验证数据是否正确

select * from #tblAddToInventory
--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    1           2           2.00        2012-07-07
--    2           3           3.00        2012-07-07
--    3           4           4.00        2012-07-07
--    2           2           2.00        2012-07-08
--    3           3           3.00        2012-07-08
select * from #tblWithdrawnFromInventory
--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    2           4           4.00        2012-07-09
--    3           5           5.00        2012-07-10
开始解决方案: --使用Union All连接查询,并将第二个查询乘以-1使其为负数

select * into #tblAddToInventory from (
select 1 as ProductID, 2 as Quantity, 2.00 as Amount, cast('7/7/2012' as date) as [Date]
union all select 2 as ProductID, 3 as Quantity, 3.00 as Amount, '7/7/2012' as Date 
union all select 3 as ProductID, 4 as Quantity, 4.00 as Amount, '7/7/2012' as Date 
union all select 2 as ProductID, 2 as Quantity, 2.00 as Amount, '7/8/2012' as Date 
union all select 3 as ProductID, 3 as Quantity, 3.00 as Amount, '7/8/2012' as Date 
) a 
select * into #tblWithdrawnFromInventory from (
select 2 as ProductID, 4 as Quantity, 4.00 as Amount, cast('7/9/2012' as date) as [Date]
union all select 3 as ProductID, 5 as Quantity, 5.00 as Amount, '7/10/2012' as Date 
) b 
select * from #tblAddToInventory union all 
select ProductID, (Quantity * -1) as Quantity, Amount, Date from #tblWithdrawnFromInventory

--    ProductID   Quantity    Amount      Date
--    ----------- ----------- ----------- ----------
--    1           2           2.00        2012-07-07
--    2           3           3.00        2012-07-07
--    3           4           4.00        2012-07-07
--    2           2           2.00        2012-07-08
--    3           3           3.00        2012-07-08
--    2           -4          4.00        2012-07-09
--    3           -5          5.00        2012-07-10

select ProductID, sum(Quantity) as Quantity, sum(Amount) as Amount from (
    select * from #tblAddToInventory union all 
    select ProductID, (Quantity * -1) as Quantity, (Amount * -1) as Amount, Date from #tblWithdrawnFromInventory
) joinedData
where [Date] >= '7/6/2012' and [Date] <= '7/11/2012'
group by ProductID

--    ProductID   Quantity    Amount
--    ----------- ----------- -----------
--    1           2           2.00
--    2           1           1.00
--    3           2           2.00

为什么不让它成为一个InventoryActivity表,可以有正数或负数的变化?尽管如此,您仍然可以在两个select查询之间使用并集,然后对这两个查询的结果求和。您可以使用linq或lambda选择具有相同idI的每一行上的值。我还不太擅长sql。您能告诉我select命令的语法吗?为什么不将其设置为一个InventoryActivity表,该表可以有正数或负数的更改?尽管如此,您仍然可以在两个select查询之间使用并集,然后对这两个查询的结果求和。您可以使用linq或lambda选择具有相同idI的每一行上的值。我还不太擅长sql。你能告诉我select命令的语法吗?谢谢trungtin1710。但是你的代码没有给我正确的结果。这就是我想到的:选择TblAddToInventory.ProductID作为[Product ID],选择SUM(TblAddToInventory.QUOTE)-SUM(TblWithdrawal.QUOTE)作为数量,选择SUM(TblAddToInventory.Amount)-SUM(TblWithdrawal.Amount)作为TblAddToInventory.ProductID=TblAddToInventory.ProductID的TblAddToInventory左侧外部联接TblWithdrawal上TblAddToInventory.ProductID组TblWithdrawal的金额,TblAddToInventory.ProductIDIt为我提供了正确的值,但对于那些没有取款的ProductID,它为我提供了空值。我需要的是给我TblAddToInventory中ProductID的总金额,如果没有从TblWithdrawnFromInventory中扣除“对于那些没有取款的ProductID,它给我一个空值”我认为这与您的情况相符,在您的示例数据中,我的代码可以求和productid=1它给我一个productid为1而不是2的空值,以及金额为空而不是2.00的值,因为不存在使用该productid求和的取款。我正在使用我在评论中创建的代码,因为我在使用您的代码时出错,但这就是我的想法来源。谢谢trungtin1710。但是你的代码没有给我正确的结果。这就是我想到的:选择TblAddToInventory.ProductID作为[Product ID],选择SUM(TblAddToInventory.QUOTE)-SUM(TblWithdrawal.QUOTE)作为数量,选择SUM(TblAddToInventory.Amount)-SUM(TblWithdrawal.Amount)作为TblAddToInventory.ProductID=TblAddToInventory.ProductID的TblAddToInventory左侧外部联接TblWithdrawal上TblAddToInventory.ProductID组TblWithdrawal的金额,TblAddToInventory.ProductIDIt为我提供了正确的值,但对于那些没有取款的ProductID,它为我提供了空值。我需要的是给我TblAddToInventory中ProductID的总金额,如果没有从TblWithdrawnFromInventory中扣除“对于那些没有取款的ProductID,它给我一个空值”我认为这与您的情况相符,在您的示例数据中,我的代码可以求和productid=1它给我一个productid为1而不是2的空值,以及金额为空而不是2.00的值,因为不存在使用该productid求和的取款。我正在使用我在注释中创建的代码,因为我在使用您的代码时会出错,但这就是我的想法来源。