如何创建存储过程来计算SQL Server 2005中的初始库存,库存,库存和库存余额

时间:2012-01-17 05:58:45

标签: sql sql-server sql-server-2005

我有3张桌子:

  • ProductProduct_Model, Product_Color, Product_Code
  • StockInStockIn_Date, Product_Code, Product_SerialNo
  • StockOutStockOut_Date, Product_SerialNo

我想创建一个存储过程来生成包含以下需求字段的报告:

  • (开始stockstock instock outstock balance
  • 分组依据(Product_ModelProduct_ColorProduct_Code
  • 按2个参数过滤:@StartDate@EndDate

如何使用此方案创建存储过程?

2 个答案:

答案 0 :(得分:0)

假设您的表 StockIn StockOut 有一列金额,相反,这个问题毫无意义。

所以,丑陋和简单的查询是:

SELECT 
  BeginningStockIn
  IsNull(BeginningStockIn.Amount, 0)-IsNull(BeginningStockOut.Amount, 0) BeginningStock,
  IsNull(PeriodStockIn.Amount, 0) StockIn,
  IsNull(PeriodStockOut.Amount, 0) StockOut,
  IsNull(BeginningStockIn.Amount, 0)-IsNull(BeginningStockOut.Amount, 0)+IsNull(PeriodStockIn.Amount, 0)-IsNull(PeriodStockOut.Amount, 0) StockBalance
FROM Product
LEFT JOIN
(
  SELECT 
    SUM(Amount) Amount,
    Product_Code
  FROM StockIn
  WHERE StockIn_Date < @StartDate
) BeginningStockIn ON BeginningStockIn.Product_Code = Product.Product_Code
LEFT JOIN
(
  SELECT 
    SUM(Amount) Amount,
    Product_Code
  FROM StockOut
  WHERE StockOut_Date < @StartDate
) BeginningStockOut ON BeginningStockOut.Product_Code = Product.Product_Code
LEFT JOIN
(
  SELECT 
    SUM(Amount) Amount,
    Product_Code
  FROM StockIn
  WHERE StockIn_Date >= @StartDate AND StockIn_Date < @EndDate
) PeriodStockIn ON PeriodStockIn .Product_Code = Product.Product_Code
LEFT JOIN
(
  SELECT 
    SUM(Amount) Amount,
    Product_Code
  FROM StockOut
  WHERE StockOut_Date >= @StartDate AND StockOut_Date < @EndDate
) PeriodStockOut ON PeriodStockOut.Product_Code = Product.Product_Code

答案 - 要创建存储过程,您必须使用here所述的CREATE PROCEDURE语句

CREATE PROC YourProcName
  @StartDate datetime,
  @EndDate datetime
AS
BEGIN
  SET NOCOUNT ON;

  the query

END

答案 1 :(得分:0)

H,

所有归功于Oleg Dok发布上述剧本。

以下是我正在使用的数据:

enter image description here


以下是代码:

declare @startdate date = '2012-01-02'
declare @enddate date = '2012-01-31'

SELECT 
  Product.Product_Code,
  Product.Product_Color,
  Product.Product_Model,
  IsNull(BeginningStockIn.Amount, 0)-IsNull(BeginningStockOut.Amount, 0) BeginningStock,
  IsNull(PeriodStockIn.Amount, 0) StockIn,
  IsNull(PeriodStockOut.Amount, 0) StockOut,
  IsNull(BeginningStockIn.Amount, 0)-IsNull(BeginningStockOut.Amount, 0)+IsNull(PeriodStockIn.Amount, 0)-IsNull(PeriodStockOut.Amount, 0) StockBalance
FROM Product
LEFT JOIN
(
  SELECT 
    SUM(Amount) Amount,
    Product_Code
  FROM StockIn
  WHERE StockIn_Date < @StartDate
  group by Product_Code
) BeginningStockIn ON BeginningStockIn.Product_Code = Product.Product_Code
LEFT JOIN
(
  SELECT 
    SUM(Amount) Amount,
    Product_Code
  FROM StockOut
  WHERE StockOut_Date < @StartDate
    group by Product_Code
) BeginningStockOut ON BeginningStockOut.Product_Code = Product.Product_Code
LEFT JOIN
(
  SELECT 
    SUM(Amount) Amount,
    Product_Code
  FROM StockIn
  WHERE StockIn_Date >= @StartDate AND StockIn_Date < @EndDate
    group by Product_Code
) PeriodStockIn ON PeriodStockIn .Product_Code = Product.Product_Code
LEFT JOIN
(
  SELECT 
    SUM(Amount) Amount,
    Product_Code
  FROM StockOut
  WHERE StockOut_Date >= @StartDate AND StockOut_Date < @EndDate
    group by Product_Code
) PeriodStockOut ON PeriodStockOut.Product_Code = Product.Product_Code

这是输出:

Product_Code    Product_Color   Product_Model   BeginningStock  StockIn StockOut    StockBalance
1   red 123 5   0   3   2
2   red 456 10  0   3   7

这是你想要的吗?