加入两个表并使用两个表在日期范围之间获取数据

时间:2015-08-29 13:32:23

标签: sql sql-server

我创建了两个表

  1. Item_In
  2. Item_Out
  3. 使用此代码:

    CREATE TABLE Item_In
    (
        Id INT PRIMARY KEY,
        ItemId INT,
        ItemCode VARCHAR(40), 
        Name VARCHAR(40),
        Price MONEY,
        Quanity INT, 
        InDate DATE
    )
    
    CREATE TABLE Item_Out
    (
        Id INT PRIMARY KEY,
        ItemId INT,
        ItemCode VARCHAR(40), 
        Name VARCHAR(40),
        SellingPrice MONEY,
        Quanity INT, 
        OutDate DATE
    )
    

    示例数据:

    insert into Item_In values('0001','C01','LAPTOP','75000','10','2015/05/10')
    insert into Item_In values('0002','E01','HD','8000','8','2015/05/10')
    insert into Item_In values('0003','MO1','RAM','5000','9','2015/06/10')
    insert into Item_In values('0004','W01','MOUSE','250','12','2015/05/10')
    insert into Item_In values('0001','C01','LAPTOP','75000','9','2015/06/25')
    insert into Item_In values('0001','C01','LAPTOP','75000','8','2015/04/10')
    insert into Item_In values('0002','E01','HD','8000','6','2015/08/10')
    
    
    insert into Item_Out values('0002','E01','HD','8000','2','2015/06/18')
    insert into Item_Out values('0001','C01','LAPTOP','75000','2','2015/05/20')
    insert into Item_Out values('0002','E01','HD','8000','3','2015/05/11')   
    insert into Item_Out values('0003','MO1','RAM','5000','2','2015/06/15')
    insert into Item_Out values('0004','W01','MOUSE','250','1','2015/05/16')
    insert into Item_Out values('0001','C01','LAPTOP','75000','3','2015/06/29')
    insert into Item_Out values('0001','C01','LAPTOP','75000','1','2015/04/19')
    

    ID是一个自动增量列。

    我想加入这两个表,并在日期范围之间获取数据,总和为QuantityBalance Quantity

    我希望得到跟进,我使用日期范围为2015/04/01和2015/06/30。在下面我只输入两个记录,

    ItemId | ItemCode | QuantityIN | QuantityOut | BalanceQuantity
    -------+----------+------------+-------------+---------------   
    001    |C01       |27          |6            |21
    002    |E01       |8           |5            |3
    

    我尝试使用波纹管代码,但它没有按照我的意愿工作。

    SELECT 
        dbo.Item_In.ItemId, dbo.Item_In.ItemCode, 
        SUM(dbo.Item_In.Quanity) AS Expr2, 
        SUM(dbo.Itenm_Out.Quanity) AS Expr1
    FROM   
        dbo.Item_In 
    INNER JOIN 
        dbo.Itenm_Out ON dbo.Item_In.Id = dbo.Itenm_Out.Id
    GROUP BY 
        dbo.Item_In.ItemId, dbo.Item_In.ItemCode
    ORDER BY 
        dbo.Item_In.ItemId
    

2 个答案:

答案 0 :(得分:0)

试一试

SELECT max(dbo.Item_In.ItemId) as 'Item_ID', max(dbo.Item_In.ItemCode) as 'Item_Code', SUM(dbo.Item_In.Quanity) AS QtyIn, SUM(dbo.Item_Out.Quanity) AS QtyOut,(SUM(dbo.Item_In.Quanity) - SUM(dbo.Item_Out.Quanity)) as Balance
FROM   dbo.Item_In INNER JOIN dbo.Itenm_Out ON dbo.Item_In.Id = dbo.Itenm_Out.Id
where indate between '2015-04-01' and '2015-06-30' and outdate between '2015/04/01' and '2015/06/30'
GROUP BY dbo.Item_In.ItemId, dbo.Item_In.ItemCode
ORDER BY dbo.Item_In.ItemId

答案 1 :(得分:0)

这比看起来更难。您需要将两个表中的库存相加,将它们组合起来以避免重复并计算所有内容。因此,以下给出了每个日期的总和:

select ItemId, ItemCode, thedate, sum(inq) as inq, sum(outq) as outq
from ((select i.ItemId, i.ItemCode, i.inDate as thedate, i.Quantity as inq, 0 as outq
       from item_in i
      ) union all
      (select o.ItemId, o.ItemCode, o.inDate as thedate, 0, - o.Quantity
       from item_in i
      )
     ) io;

但是,您的最终答案需要累计金额。在SQL Server 2012+中,您可以这样做:

select ItemId, ItemCode, thedate,
       sum(sum(inq)) over (partition by ItemId order by thedate) as inq, 
       sum(sum(outq)) over (partition by ItemId order by thedate) as outq, 
       (sum(sum(inq)) over (partition by ItemId order by thedate) - 
        sum(sum(outq)) over (partition by ItemId order by thedate)
       ) as balance
from ((select i.ItemId, i.ItemCode, i.inDate as thedate, i.Quantity as inq, 0 as outq
       from item_in i
      ) union all
      (select o.ItemId, o.ItemCode, o.inDate as thedate, 0, o.Quantity
       from item_in i
      )
     ) io;

在早期版本的SQL Server中,您必须采用不同的方法。