Sql 如何保持运行的总计列但按条件停止
我需要满足一些产品的数量。 假设我有100条产品Foo的记录,每条记录的数量在1-100之间。 我刚收到25份Foo的订单 我当然可以把所有的记录都取回来,只取我需要的,但我想知道如何返回足够的记录来满足请求 我相信我已经很接近了,但我无法找出如何限制我达到目标后的记录Sql 如何保持运行的总计列但按条件停止,sql,tsql,Sql,Tsql,我需要满足一些产品的数量。 假设我有100条产品Foo的记录,每条记录的数量在1-100之间。 我刚收到25份Foo的订单 我当然可以把所有的记录都取回来,只取我需要的,但我想知道如何返回足够的记录来满足请求 我相信我已经很接近了,但我无法找出如何限制我达到目标后的记录 declare @neededQty = 10 ;WITH Products as ( Select p.Name, sp.Price, sp.Quantity, SUM(sp.Quant
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
您需要找到运行总数中您想要的数量更多的地方。试试这个:
您可以使用这个谓词
where Name = 'Foo'
and QtyRunningTotal - Quantity < @neededQty
我计算出,给定数据集,您需要一个范围。我还假设您希望首先收回尽可能少的产品数量,即如果我只需要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
你能只取前1条记录吗?你用的是哪种数据库管理系统?Oracle、MS SQLServer等?最后一个我没有意识到的用例可能是您可以提供帮助。不确定是否应创建新帖子,但如果无法满足数量要求,我不希望返回任何行。然后,您还需要一个附加列,用于总计SUMsp.QuantityOVERPARTITION(按p进行)。名称为QtyTotal并筛选QtyTotal>@needdqtyperfect,再次!
where Name = 'Foo'
and QtyRunningTotal - Quantity < @neededQty
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