与sql中各列的减法相加

时间:2015-06-04 05:06:59

标签: sql-server sql-server-2008

我必须显示所有位置的当前库存以及位置名称。有四个表格表格设置包含分支详细信息,Stockinward包含位置库存以及产品ID,Product Outward包含特定产品的产品销售,ProductMaster包含产品的deatils。这是我的样本记录,我尝试了以下功能

CREATE FUNCTION [dbo].[cstock]

(

  @sID int

)

RETURNS VARCHAR(MAX)

AS

BEGIN
DECLARE @stckprdt int,@stckprdt1 int,@stockloc varchar(Max);
   select @stckprdt= SUM(Qty)from StockInward where ProductId= @sID group by  StockLocation
  select @stckprdt1=SUM(Qty)from ProductOutward where ProductId= @sID group by Location

   RETURN @stckprdt-@stckprdt1
END

GO

ProductMaster

ProductId ProductName

1          WashingMachine
2           Fridge
3            Tv
4             Laptop

设置

Id  BranchName

1      Chennai

2      Coimbatore

3      Mumbai

4     Cochin

StockInward

Id  ProductId    StockLocation    Qty

1      1             4             100
2      4             4              10
3      4              1             10
4      3              2             20
5      `1             1             10

ProductOutward

Id  ProductId    StockLocation    Qty

1        4          4              2
2         1         4              2

输出应为

orderedproducts       currentstock

Laptop(2)               Cochin(8)
                         Chennai(10)

WashingMachine(2)            Cochin(98)
                           Chennai(10)

在输出中,currentstock应显示特定地点的产品总和来自StockInward -sum产品的特定地点的产品总和来自StockOutward

2 个答案:

答案 0 :(得分:1)

您不需要某个功能,您可以使用这样的选择语句:

SELECT COALESCE(ProductName + 
                '(' + CAST(po.Qty as varchar(10)) + ')', 
                '')  As orderedproducts, 
       BranchName +
       '(' + CAST(si.Qty - COALESCE(po.Qty, 0) As varchar(10)) +')' As CurrentStock 
FROM ProductMaster pm 
INNER JOIN StockInward si ON(pm.ProductId = si.ProductId)
INNER JOIN Setup s ON(si.StockLocation = s.Id)
LEFT JOIN ProductOutward po ON(pm.ProductId = po.ProductId AND po.StockLocation = s.Id)
WHERE EXISTS (
  SELECT 1
  FROM ProductOutward
  WHERE ProductId = pm.ProductId
)
ORDER BY ProductName, orderedproducts DESC;

see fiddle here

<强>说明:ProductMasterStockInwardSetup之间使用内部联接为我提供了所有位置的所有产品。
使用该ProductOutward之间的左连接,我可以将ProductOutward.Qty添加到产品名称中 利用将null值连接到sql server中的字符串的事实返回null值,我确保只有实际销售的产品才会出现在结果集中。
使用coalesce将空值显示为空字符串给出了确切的结果,因为描述了所需的结果。

<强>更新
要正确计算StockInward和/或ProductOutward中存在同一产品和位置的多个记录的值,请将查询更改为直接使用派生表而不是这些表,如下所示:

SELECT COALESCE(ProductName + 
                '(' + CAST(po.Qty as varchar(10)) + ')', 
                '')  As orderedproducts, 
       BranchName +
       '(' + CAST(si.Qty - COALESCE(po.Qty, 0) As varchar(10)) +')' As CurrentStock 
FROM ProductMaster pm 
INNER JOIN (
    SELECT ProductId, StockLocation, SUM(Qty) As Qty
    FROM StockInward 
    GROUP BY ProductId, StockLocation
) si ON(pm.ProductId = si.ProductId)
INNER JOIN Setup s ON(si.StockLocation = s.Id)
LEFT JOIN (
    SELECT ProductId, StockLocation, SUM(Qty) As Qty
    FROM ProductOutward 
    GROUP BY ProductId, StockLocation
) po ON(pm.ProductId = po.ProductId AND po.StockLocation = s.Id)
WHERE EXISTS (
  SELECT 1
  FROM ProductOutward
  WHERE ProductId = pm.ProductId
)
ORDER BY ProductName, orderedproducts DESC;

注意:您可能应该为StockInward和ProductOutward添加一个日期列,以便过去询问库存。
目前,您的数据库设计仅允许处理当前库存 您甚至无法判断订单是在库存到达该地点之前还是之后放置的。

see fiddle here

答案 1 :(得分:0)

你能试试吗

select ProductName + '('+convert(nvarchar(10),ProductOutward.Qty)+')' as orderedproducts, BranchName + '('+convert(nvarchar(10),StockInward.Qty-ProductOutward.Qty)+')' as currentstock
from ProductMaster 
inner join ProductOutward on ProductMaster.Productid = ProductOutward.productid
inner join stockinward on ProductMaster.Productid = stockinward.productid and stockinward.StockLocation = ProductOutward.StockLocation
inner join Setup on stockinward.stocklocation = Setup.id
union
select ProductName + '('+convert(nvarchar(10),0)+')' as orderedproducts, BranchName + '('+convert(nvarchar(10),StockInward.Qty)+')' as currentstock
from ProductMaster 
inner join ProductOutward on ProductMaster.Productid = ProductOutward.productid
inner join stockinward on ProductMaster.Productid = stockinward.productid and stockinward.StockLocation <> ProductOutward.StockLocation
inner join Setup on stockinward.stocklocation = Setup.id

Fiddle

编辑:对于多个Stockinwards&amp; ProductOutwards

select ProductName + '('+convert(nvarchar(10),sum(ProductOutward.Qty)/count(distinct stockinward.id))+')' as orderedproducts
, BranchName + '('+convert(nvarchar(10),sum(StockInward.Qty)/count(distinct ProductOutward.id)-sum(ProductOutward.Qty)/count(distinct stockinward.id))+')' as currentstock

from ProductMaster 
inner join ProductOutward on ProductMaster.Productid = ProductOutward.productid
inner join stockinward on ProductMaster.Productid = stockinward.productid and stockinward.StockLocation = ProductOutward.StockLocation
inner join Setup on stockinward.stocklocation = Setup.id

where ProductName = 'laptop'
group by productname,Setup.BranchName--,stockinward.id


union

select ProductName + '('+convert(nvarchar(10),0)+')' as orderedproducts, BranchName + '('+convert(nvarchar(10),sum(StockInward.Qty))+')' as currentstock
from ProductMaster 
inner join ProductOutward on ProductMaster.Productid = ProductOutward.productid
inner join stockinward on ProductMaster.Productid = stockinward.productid and stockinward.StockLocation <> ProductOutward.StockLocation
inner join Setup on stockinward.stocklocation = Setup.id
where ProductName = 'laptop'
group by ProductName,ProductOutward.Qty,BranchName

检查出来:

select ProductName + '('+convert(nvarchar(10),sum(ProductOutward.Qty)/count(distinct stockinward.id))+')' as orderedproducts
, BranchName + '('+convert(nvarchar(10),sum(StockInward.Qty)/count(distinct ProductOutward.id)-sum(ProductOutward.Qty)/count(distinct stockinward.id))+')' as currentstock

from ProductMaster 
inner join ProductOutward on ProductMaster.Productid = ProductOutward.productid
inner join stockinward on ProductMaster.Productid = stockinward.productid and stockinward.StockLocation = ProductOutward.StockLocation
inner join Setup on stockinward.stocklocation = Setup.id

where ProductName = 'laptop'
group by productname,Setup.BranchName--,stockinward.id

union 

select ProductName + '('+convert(nvarchar(10),sum(ProductOutward.Qty))+')' as orderedproducts
, BranchName + '(0)' as currentstock

from ProductMaster 
inner join ProductOutward on ProductMaster.Productid = ProductOutward.productid
inner join Setup on ProductOutward.stocklocation = Setup.id

where ProductName = 'laptop' and ProductMaster.ProductId not in (select stockinward.productid from stockinward where stockinward.stocklocation = ProductOutward.stocklocation)
group by productname,Setup.BranchName--,stockinward.id

union 

select ProductName + '(0)' as orderedproducts
, BranchName + '('+convert(nvarchar(10),sum(stockinward.Qty))+')' as currentstock

from ProductMaster 
inner join stockinward on ProductMaster.Productid = stockinward.productid
inner join Setup on stockinward.stocklocation = Setup.id

where ProductName = 'laptop' and ProductMaster.ProductId not in (select ProductOutward.productid from ProductOutward where ProductOutward.stocklocation = stockinward.stocklocation)
group by productname,Setup.BranchName--,stockinward.id