表: StoreItems
表: StockInOutType
表: Store
我想计算股票进入时间和结束时间。
我的查询
SELECT
ItemName, DateTime [Entry Time], Qty
FROM
Store
JOIN
StockInOutType ON Store.StockInOutType = StockInOutType.Id
JOIN
StoreItems ON Store.ItemID = StoreItems.ItemID
WHERE
Store.StockInOutType = 4
AND StoreItems.ItemID = 25
AND SessionID = 735721
StockInOuttype
4和6不是销售。
数据(Store
表)
必填项:
ItemName EntryTime Quantity EndingTime
Chicken Biryani 2015-09-03 10:16:09 1 2015-09-03 10:18:20
Chicken Biryani 2015-09-03 10:21:26 10 2015-09-03 10:22:17
Chicken Biryani 2015-09-03 10:26:25 15 2015-09-03 10:57:18
Chicken Biryani 2015-09-03 10:26:57 10 null
答案 0 :(得分:2)
最大的问题是,如果购买了“购买者”,您希望发生什么?活动时仍有库存。由于您没有来自“销售”的链接。对股票这种行为很难跟踪。假设这没有发生/您不关心这种情况,解决方案是简单地选择下一个库存作为书挡如下
SELECT ItemName,
DateTime AS [Entry Time],
Qty,
CASE WHEN LEAD(Store.DateTime) OVER (ORDER BY Store.DateTime) IS NOT NULL
THEN (SELECT MAX(DateTime)
FROM Store s2
WHERE s2.StockInOutType = 2 AND
s2.DateTime < LEAD(Store.DateTime) OVER (ORDER BY Store.DateTime))
ELSE NULL END AS EndingTime
FROM
Store
WHERE
Store.StockInOutType = 4
AND Store.ItemID = 25
ORDER BY DateTime
答案 1 :(得分:1)
您可以通过计算每个购买交易的起点和出口点的序列号来计算FIFO模式中的库存周转率。
这可以通过使用运行总计(-- Calculate serials for the orders and inventory receipts
-- put them in temp tables to make them easier to work with. This could
-- also be done with a view or a CTE
CREATE TABLE #Orders (
TxnID INT NOT NULL,
TxnDate DATETIME NOT NULL,
ItemID INT NOT NULL,
Qty INT NOT NULL,
ConsumptionStartSerial INT,
ConsumptionEndSerial INT
);
INSERT INTO #Orders (TxnID, TxnDate, ItemID, Qty, ConsumptionStartSerial, ConsumptionEndSerial)
SELECT TxnID, TxnDate, ItemID, Qty, ConsumptionEndSerial - Qty as ConsumptionStartSerial, ConsumptionEndSerial
FROM (
SELECT TxnID, TxnDate, ItemID, ChangeQty * -1 as Qty,
SUM(ChangeQty) OVER(PARTITION BY ItemID ORDER BY TxnID ROWS UNBOUNDED PRECEDING) * -1 as ConsumptionEndSerial
FROM ItemTransactions
WHERE ChangeQty < 0
) as inr;
SELECT * FROM #Orders;
CREATE TABLE #InventoryReceipts (
TxnID INT NOT NULL,
ItemID INT NOT NULL,
Qty INT NOT NULL,
StockStartSerial INT,
StockEndSerial INT
);
INSERT INTO #InventoryReceipts (TxnID, ItemID, Qty, StockStartSerial, StockEndSerial)
SELECT TxnID, ItemID, Qty, StockEndSerial - Qty as StockStartSerial, StockEndSerial
FROM (
SELECT TxnID, ItemID, ChangeQty as Qty,
SUM(ChangeQty) OVER(PARTITION BY ItemID ORDER BY TxnID ROWS UNBOUNDED PRECEDING) as StockEndSerial
FROM ItemTransactions
WHERE ChangeQty > 0
) as inr;
SELECT * FROM #InventoryReceipts;
-- Stock turnover
-- Find the first and last order for each inventory receipt
-- Calculate the days on shelf based off of the first and last
-- order which uses the product
SELECT inr.TxnID as StockTxn, txn.TxnDate, txn.[Description], inr.ItemID, inr.Qty, i.Name,
inr.StockOpenedTxnID, inr.StockFinishedTxnID,
DATEDIFF(day, txn.TxnDate, oStart.TxnDate) as DaysToOpen,
DATEDIFF(day, txn.TxnDate, oEnd.TxnDate) as DaysToConsumed
FROM (
SELECT ir.TxnID, ir.ItemID, ir.Qty, MAX(oStart.TxnID) as StockOpenedTxnID, MIN(oEnd.TxnID) as StockFinishedTxnID
FROM #InventoryReceipts ir
LEFT OUTER JOIN #Orders oStart on ir.ItemID = oStart.ItemID and ir.StockStartSerial >= oStart.ConsumptionStartSerial
LEFT OUTER JOIN #Orders oEnd on ir.ItemID = oEnd.ItemID and ir.StockEndSerial < oEnd.ConsumptionEndSerial
GROUP BY ir.TxnID, ir.ItemID, ir.Qty
) as inr
INNER JOIN Items i on inr.ItemID = i.ItemID
INNER JOIN ItemTransactions txn on inr.TxnID = txn.TxnID
LEFT OUTER JOIN #Orders oStart ON inr.StockOpenedTxnID = oStart.TxnID
LEFT OUTER JOIN #Orders oEnd ON inr.StockFinishedTxnID = oEnd.TxnID
DROP TABLE #InventoryReceipts;
DROP TABLE #Orders;
)
示例(my post):
| StockTxn | TxnDate | Description | ItemID | Qty | Name | StockOpenedTxnID | StockFinishedTxnID | DaysToOpen | DaysToConsumed |
|----------|---------------------------|---------------------------|--------|-----|---------|------------------|--------------------|------------|----------------|
| 1 | January, 01 2015 00:00:00 | Received 10 from supplier | 1 | 10 | Chicken | 3 | 6 | 8 | 12 |
| 2 | January, 02 2015 00:00:00 | Received 5 from supplier | 2 | 5 | Beef | 5 | (null) | 9 | (null) |
| 4 | January, 11 2015 00:00:00 | Received 5 from supplier | 1 | 5 | Chicken | 6 | (null) | 2 | (null) |
<强> SqlFiddle 强>:
ngModel.$validators.pattern = function($modelValue, $viewValue) {
var pattern = /^\d*$/; // whatever your pattern is
return pattern.test($viewValue);
};