我有3张桌子:
Product
(Product_Model, Product_Color, Product_Code
)StockIn
(StockIn_Date, Product_Code, Product_SerialNo
)StockOut
(StockOut_Date, Product_SerialNo
)我想创建一个存储过程来生成包含以下需求字段的报告:
stock
,stock in
,stock out
,stock balance
)Product_Model
,Product_Color
,Product_Code
)@StartDate
和@EndDate
如何使用此方案创建存储过程?
答案 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发布上述剧本。
以下是我正在使用的数据:
以下是代码:
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
这是你想要的吗?