如何保持运行总列但按条件停止

时间:2019-11-05 01:34:47

标签: sql tsql

我需要为一些产品提供一些数量。 假设我有100条产品Foo的记录,每条记录的数量在1-100之间。 我刚订购了25数量的Foo。

我当然可以拉回所有记录,然后取出需要的东西,但我想弄清楚如何返回足够多的记录来满足请求。

我相信我距离这里很近,但是我一经达成目标就无法弄清楚如何限制返回的记录。

declare @neededQty = 10

;WITH Products as 
(
Select 
    p.Name, 
    sp.Price,
    sp.Quantity,
    SUM(sp.Quantity)OVER(PARTITION BY p.Name order by sp.Price, sp.Quantity desc) as QtyRunningTotal
from Product p
    inner join ProductDetails sp on sp.ProductId = p.ProductId
where productId IN (1,2,3,4)
)
select 
* from Products
where QtyRunningTotal >= @neededQty

这是当前输出,但是我希望它只返回第一个Foo记录,因为我只需要10个数量

Name    Price   Quantity    QtyRunningTotal
--------------------------------------------
Foo     1       15          15
Foo     1       15          30
Foo     1       15          45
Foo     1       15          60
Foo     1       100         160
Foo     1       100         260
Foo     1       100         360
Bar     1       10          10
Bar     1       10          20
Bar     1       10          30

3 个答案:

答案 0 :(得分:1)

您可以使用此谓词。

where Name = 'Foo' 
      and QtyRunningTotal - Quantity < @neededQty 

答案 1 :(得分:0)

您需要找到总计中想要更多数量的地方。试试这个:

;WITH Products as 
(
Select 
    p.Name, 
    sp.Price,
    sp.Quantity,
    SUM(sp.Quantity)OVER(PARTITION BY p.Name order by sp.Price, sp.Quantity desc) as QtyRunningTotal
from Product p
    inner join ProductDetails sp on sp.ProductId = p.ProductId
where productId IN (1,2,3,4)
)
select 
* from Products
where neededQty >= ABS(neededQty - QtyRunningTotal)
Name    Price   Quantity    QtyRunningTotal   Required  total-req 
------------------------------------------------------------------
Foo     1       15          15                10        -5        
Foo     1       15          30                10        -20        
Foo     1       15          45                10        -35        
Foo     1       15          60                10        -50        
Foo     1       100         160               10        -150        
Foo     1       100         260               10        -250        
Foo     1       100         360               10        -350        
Bar     1       10          10                10        0        
Bar     1       10          20                10        -10        
Bar     1       10          30                10        -20        

答案 2 :(得分:0)

我计算出,根据您的数据集,您将需要一个范围。我还假设您想先拉回尽可能少的产品,即,如果我只需要15件商品,我会拉回记录说10和11,而不是拉回记录100。这是一个选择:

CREATE TABLE #Product(ProductID int, ProdName varchar(20))
CREATE TABLE #ProductDetails(ProductID int, Price int, Quantity int)

INSERT INTO #Product(ProductID, ProdName) VALUES(1, 'FOO')
INSERT INTO #Product(ProductID, ProdName) VALUES(2, 'BAR')

INSERT INTO #ProductDetails(ProductID,Price,Quantity) VALUES(1,1,15)
INSERT INTO #ProductDetails(ProductID,Price,Quantity) VALUES(1,1,16)
INSERT INTO #ProductDetails(ProductID,Price,Quantity) VALUES(1,1,17)
INSERT INTO #ProductDetails(ProductID,Price,Quantity) VALUES(1,1,18)
INSERT INTO #ProductDetails(ProductID,Price,Quantity) VALUES(1,1,100)
INSERT INTO #ProductDetails(ProductID,Price,Quantity) VALUES(1,1,101)
INSERT INTO #ProductDetails(ProductID,Price,Quantity) VALUES(1,1,102)

INSERT INTO #ProductDetails(ProductID,Price,Quantity) VALUES(2,1,10)
INSERT INTO #ProductDetails(ProductID,Price,Quantity) VALUES(2,1,11)
INSERT INTO #ProductDetails(ProductID,Price,Quantity) VALUES(2,1,12)

declare @neededQty int = 32

;WITH Products as 
(
Select 
    p.ProdName, 
    sp.Price,
    sp.Quantity,
     LAG(sp.Quantity,1,0) OVER(PARTITION BY p.ProdName order by sp.Price, sp.Quantity  ) LG
from #Product p
    inner join #ProductDetails sp on sp.ProductId = p.ProductId
where p.productId IN (1,2,3,4)
), Prod2 AS
(
select * , 
SUM(LG) OVER(PARTITION BY ProdName ORDER BY Price, LG ) LowRange
,SUM(Quantity) OVER(PARTITION BY ProdName ORDER BY Price, Quantity ) HighRange
from Products)

SELECT * 
FROM Prod2
WHERE LowRange < @neededQty OR  @neededQty >= HighRange