これは愚かな Northwind の例です。
Select
ods.OrderID
, derived1.Quantity
, derived1.Discount
, derived1.SETKey
, TotalsItemsInThisSet = derived1.MYC
, ThisSetCardinal = ROW_NUMBER() OVER(PARTITION BY derived1.Quantity, derived1.Discount ORDER BY derived1.Quantity, derived1.Discount)
from
dbo.[Order Details] ods
join
( select Quantity, Discount , COUNT(*) as MYC , ROW_NUMBER() OVER (Order by Quantity, Discount) AS SETKey from dbo.[Order Details] where Quantity > 50 and Discount > 0 GROUP BY Quantity, Discount Having Count(*) > 1 ) derived1
on ods.Quantity = derived1.Quantity and ods.Discount = derived1.Discount
order by
derived1.Quantity, derived1.Discount
結果:
OrderID Quantity Discount SETKey TotalsItemsInThisSet ThisSetCardinal
----------- -------- ------------- -------------------- -------------------- --------------------
10361 55 0.1 1 2 1
10451 55 0.1 1 2 2
10269 60 0.05 2 9 1
10273 60 0.05 2 9 2
10419 60 0.05 2 9 3
10492 60 0.05 2 9 4
10570 60 0.05 2 9 5
10590 60 0.05 2 9 6
10637 60 0.05 2 9 7
10865 60 0.05 2 9 8
11012 60 0.05 2 9 9
10390 60 0.1 3 4 1
10485 60 0.1 3 4 2
10688 60 0.1 3 4 3
10845 60 0.1 3 4 4
10475 60 0.15 4 4 1
10693 60 0.15 4 4 2
10817 60 0.15 4 4 3
10990 60 0.15 4 4 4
10424 60 0.2 5 4 1
10567 60 0.2 5 4 2
10700 60 0.2 5 4 3
10847 60 0.2 5 4 4
10263 60 0.25 6 7 1
10263 60 0.25 6 7 2
10461 60 0.25 6 7 3
10802 60 0.25 6 7 4
10912 60 0.25 6 7 5
10918 60 0.25 6 7 6
11030 60 0.25 6 7 7
10854 65 0.15 7 2 1
10990 65 0.15 7 2 2
10339 70 0.05 8 6 1
10359 70 0.05 8 6 2
10605 70 0.05 8 6 3
10658 70 0.05 8 6 4
10658 70 0.05 8 6 5
11008 70 0.05 8 6 6
10395 70 0.1 9 3 1
10845 70 0.1 9 3 2
11033 70 0.1 9 3 3
10267 70 0.15 10 4 1
10324 70 0.15 10 4 2
10403 70 0.15 10 4 3
10543 70 0.15 10 4 4
10430 70 0.2 11 2 1
10773 70 0.2 11 2 2
10344 70 0.25 12 3 1
10372 70 0.25 12 3 2
10393 70 0.25 12 3 3
10359 80 0.05 13 3 1
10472 80 0.05 13 3 2
10865 80 0.05 13 3 3
10516 80 0.1 14 2 1
10765 80 0.1 14 2 2
10324 80 0.15 15 2 1
10633 80 0.15 15 2 2
10373 80 0.2 16 2 1
10847 80 0.2 16 2 2
10515 84 0.15 17 2 1
10983 84 0.15 17 2 2
10549 100 0.15 18 2 1
10854 100 0.15 18 2 2
11030 100 0.25 19 2 1
11030 100 0.25 19 2 2
10776 120 0.05 20 2 1
10894 120 0.05 20 2 2
10398 120 0.1 21 2 1
10451 120 0.1 21 2 2