Sql server K你们已经把猪的重量设为22,在这种情况下,我不希望猪被退回。此外,在@item中,您似乎没有使用numvalue执行任何操作。您可能会发现CLR解决方案的性能最好按权重降序处理@selection中的每一行,如果i.numValues==0,则按权重降序
Sql server K你们已经把猪的重量设为22,在这种情况下,我不希望猪被退回。此外,在@item中,您似乎没有使用numvalue执行任何操作。您可能会发现CLR解决方案的性能最好按权重降序处理@selection中的每一行,如果i.numValues==0,则按权重降序,sql-server,sql-server-2012,cursor,common-table-expression,priority-queue,Sql Server,Sql Server 2012,Cursor,Common Table Expression,Priority Queue,K你们已经把猪的重量设为22,在这种情况下,我不希望猪被退回。此外,在@item中,您似乎没有使用numvalue执行任何操作。您可能会发现CLR解决方案的性能最好按权重降序处理@selection中的每一行,如果i.numValues==0,则按权重降序处理@item(i)中的每一行,然后移动到下一个i如果s.weight>i.weight移动到下一个s,则输出s,i.numValues-- ╔════════╦═══════════╗ ║ weight ║ numValues ║ ╠════
K你们已经把猪的重量设为22,在这种情况下,我不希望猪被退回。此外,在@item中,您似乎没有使用numvalue执行任何操作。您可能会发现CLR解决方案的性能最好<代码>按权重降序处理@selection中的每一行,如果i.numValues==0,则按权重降序处理@item(i)中的每一行,然后移动到下一个i如果s.weight>i.weight移动到下一个s,则输出s,i.numValues--
╔════════╦═══════════╗
║ weight ║ numValues ║
╠════════╬═══════════╣
║ 1 ║ 1 ║
║ 2 ║ 0 ║
║ 3 ║ 3 ║
╚════════╩═══════════╝
╔══════╦══════╦════════╗
║ item ║ val ║ weight ║
╠══════╬══════╬════════╣
║ 1 ║ fish ║ 1 ║
║ 2 ║ goat ║ 1 ║
║ 3 ║ cat ║ 1 ║
║ 4 ║ duck ║ 3 ║
║ 5 ║ pig ║ 2 ║
╚══════╩══════╩════════╝
╔══════╦══════╦════════╗
║ item ║ val ║ weight ║
╠══════╬══════╬════════╣
║ 1 ║ fish ║ 1 ║
║ 2 ║ goat ║ 1 ║
║ 4 ║ duck ║ 3 ║
║ 5 ║ pig ║ 2 ║
╚══════╩══════╩════════╝
declare @selection TABLE
([item] int, [val] varchar(4), [weight] int)
;
INSERT INTO @selection
([item], [val], [weight])
VALUES
(1, 'fish', 1),
(2, 'goat', 1),
(3, 'cat', 1),
(4, 'duck', 3),
(5, 'pig', 2)
;
declare @item TABLE
([weight] int, [numValues] int)
INSERT INTO @item
([weight], [numValues])
VALUES
(1, 1),
(2, 0),
(3, 3)
declare @potentialValues TABLE([item] int, [val] varchar(4), [weight] int, queueWeight int, [rn] int)
declare @maxRows INT = (SELECT SUM(numValues) FROM @item)
declare @largestQueueItem INT = (SELECT MAX(weight) from @item where numValues > 0)
;with CTE AS (
Select
s.[weight] as itemWeight,
i.[weight] as queueWeight,
item,
val,
ROW_NUMBER() OVER (PARTITION BY i.weight ORDER BY s.weight desc) AS RN
from @selection s
FULL OUTER JOIN @item i ON s.weight <= i.weight
where i.numValues > 0)
insert into @potentialValues ([item], [val], [weight], queueWeight, [rn])
select item, val, itemweight, queueWeight, rn from CTE
Where rn <= @maxRows
Declare @currentQueueItemSize INT = @largestQueueItem
while (@currentQueueItemSize > 0)
BEGIN
DECLARE @count INT = (SELECT numValues from @item where weight = @currentQueueItemSize)
; WITH T
AS (SELECT p.*
FROM @potentialValues p
WHERE p.queueWeight = @currentQueueItemSize
ORDER BY p.rn
OFFSET @count ROWS)
DELETE FROM T
DELETE p FROM @potentialValues p
INNER JOIN @potentialValues pp
ON pp.item = p.item AND p.queueWeight < @currentQueueItemSize AND pp.queueWeight = @currentQueueItemSize
SET @currentQueueItemSize = @currentQueueItemSize - 1
END
select item, val, weight from @potentialValues order by item
declare @selection TABLE
([item] int, [val] varchar(4), [weight] int)
;
INSERT INTO @selection
([item], [val], [weight])
VALUES
(1, 'fish', 1),
(2, 'goat', 1),
(3, 'cat', 1),
(4, 'duck', 3),
(5, 'pig', 2)
;
declare @item TABLE
([weight] int, [numValues] int)
INSERT INTO @item
([weight], [numValues])
VALUES
(1, 1),
(2, 0),
(3, 3)
;with CTE AS (
select T.item,t.val,tt.weight,ROW_NUMBER()OVER(PARTITION BY TT.weight ORDER BY TT.weight)RN from @selection T
FULL OUTER JOIN @item TT
ON T.weight = TT.weight)
SELECT ITEM,
VAL,
COALESCE(weight,ROW_NUMBER()over(PARTITION BY weight ORDER BY item)+1,0)
FROM CTE where ITEM IS NOT NULL AND RN <= 2
declare @selection TABLE
([item] int, [val] varchar(4), [weight] int)
;
INSERT INTO @selection
([item], [val], [weight])
VALUES
(1, 'fish', 1),
(2, 'goat', 1),
(3, 'cat', 1),
(4, 'duck', 3),
(5, 'pig', 2)
;
declare @item TABLE
([weight] int, [numValues] int)
INSERT INTO @item
([weight], [numValues])
VALUES
(1, 1),
(2, 0),
(3, 3)
declare @potentialValues TABLE([item] int, [val] varchar(4), [weight] int, queueWeight int, [rn] int)
declare @maxRows INT = (SELECT SUM(numValues) FROM @item)
declare @largestQueueItem INT = (SELECT MAX(weight) from @item where numValues > 0)
;with CTE AS (
Select
s.[weight] as itemWeight,
i.[weight] as queueWeight,
item,
val,
ROW_NUMBER() OVER (PARTITION BY i.weight ORDER BY s.weight desc) AS RN
from @selection s
FULL OUTER JOIN @item i ON s.weight <= i.weight
where i.numValues > 0)
insert into @potentialValues ([item], [val], [weight], queueWeight, [rn])
select item, val, itemweight, queueWeight, rn from CTE
Where rn <= @maxRows
Declare @currentQueueItemSize INT = @largestQueueItem
while (@currentQueueItemSize > 0)
BEGIN
DECLARE @count INT = (SELECT numValues from @item where weight = @currentQueueItemSize)
; WITH T
AS (SELECT p.*
FROM @potentialValues p
WHERE p.queueWeight = @currentQueueItemSize
ORDER BY p.rn
OFFSET @count ROWS)
DELETE FROM T
DELETE p FROM @potentialValues p
INNER JOIN @potentialValues pp
ON pp.item = p.item AND p.queueWeight < @currentQueueItemSize AND pp.queueWeight = @currentQueueItemSize
SET @currentQueueItemSize = @currentQueueItemSize - 1
END
select item, val, weight from @potentialValues order by item