Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server sql中各列的求和与减法_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql server sql中各列的求和与减法

Sql server sql中各列的求和与减法,sql-server,sql-server-2008,Sql Server,Sql Server 2008,我必须显示所有位置的当前库存以及位置名称。有四个表设置包含分支机构详细信息,Stockinward包含位置的库存以及产品Id,product Outward包含特定产品的产品销售,ProductMaster包含产品的数据。这是我的示例记录,我已经尝试了以下功能 CREATE FUNCTION [dbo].[cstock] ( @sID int ) RETURNS VARCHAR(MAX) AS BEGIN DECLARE @stckprdt int,@stckprdt1 int,

我必须显示所有位置的当前库存以及位置名称。有四个表设置包含分支机构详细信息,Stockinward包含位置的库存以及产品Id,product Outward包含特定产品的产品销售,ProductMaster包含产品的数据。这是我的示例记录,我已经尝试了以下功能

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
产品管理员

ProductId ProductName

1          WashingMachine
2           Fridge
3            Tv
4             Laptop
设置

内向股票

Id  ProductId    StockLocation    Qty

1      1             4             100
2      4             4              10
3      4              1             10
4      3              2             20
5      `1             1             10
产品出口

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到特定位置的乘积之和-从StockOutward到特定位置的乘积之和

您不需要函数,可以使用如下select语句:

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;
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;

解释: 使用
ProductMaster
stockintrown
Setup
之间的内部联接,我得到了所有位置的所有产品。
在这和
productoutfort
之间使用左连接,允许我将
productoutfort.Qty
添加到产品名称中。
在sql server中将
null
值连接到字符串会返回
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 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;
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添加一个日期列,以便能够询问过去的库存。
目前,您的数据库设计仅允许处理当前库存。
你甚至不知道订单是在库存到达该地点之前还是之后下的

你能试试这个吗

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

您希望对两个位置行重复使用笔记本电脑,还是希望该位置作为csv?您的输出不清楚。另外,你是如何得到笔记本电脑和洗衣机的?笔记本电脑(2)是我订购的产品,在产品出口表中,我订购了2台笔记本电脑和2台洗衣机。这里应该有一台(2)在洗衣机中,我也必须显示当前库存。我已经创建了函数,但它没有正确显示当前库存。解决方案是什么?在库存向内表中,有笔记本电脑10用于cochin位置,但有一个库存向外,所以它应该从库存向内分包,所以现在库存是cochin(8),而且钦奈没有股票出口,所以它仍然是一样的钦奈(10)@PravinDeshmukh:事实上,它没有失败。请注意,您在StockInward中添加了另一行,其中的值为(1,1,10)(重复),因此实际上,它返回的是should中的精确结果。@PravinDeshmukh:用于比较。先生,请帮助我..它从两个条目中提取..尝试用相同的productId复制同一位置的任何一行它从两行提取注释不用于扩展讨论;这段对话已经结束。
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