我有一个问题,我希望有一个简单的解决方案,但是,我的头撞墙砖。我有一张表,其中包含:
我曾希望将所有项目拆分成一组,其中一组中的ItemValues的总和大致相等。到目前为止,我与ntile等的努力产生了非常不平衡的群体。这是使用SQL Server 2008 R2(10.50.6220)。
非常感谢任何帮助。
创建表格的代码:
------------------------------
--Table structure for [TstGroupWeight]
------------------------------
DROP TABLE [TstGroupWeight]
GO
CREATE TABLE [TstGroupWeight]
(
[ItemID] int NOT NULL PRIMARY KEY ,
[ItemValue] int NULL ,
[Group] int NULL
)
GO
-- ----------------------------
-- Records of TstGroupWeight
-- ----------------------------
BEGIN TRANSACTION
GO
INSERT INTO [TstGroupWeight] VALUES (N'1', N'36', null);
INSERT INTO [TstGroupWeight] VALUES (N'2', N'5', null);
INSERT INTO [TstGroupWeight] VALUES (N'3', N'37', null);
INSERT INTO [TstGroupWeight] VALUES (N'4', N'11', null);
INSERT INTO [TstGroupWeight] VALUES (N'5', N'1', null);
INSERT INTO [TstGroupWeight] VALUES (N'6', N'16', null);
INSERT INTO [TstGroupWeight] VALUES (N'7', N'18', null);
INSERT INTO [TstGroupWeight] VALUES (N'8', N'14', null);
INSERT INTO [TstGroupWeight] VALUES (N'9', N'9', null);
INSERT INTO [TstGroupWeight] VALUES (N'10', N'4', null);
INSERT INTO [TstGroupWeight] VALUES (N'11', N'35', null);
INSERT INTO [TstGroupWeight] VALUES (N'12', N'37', null);
INSERT INTO [TstGroupWeight] VALUES (N'13', N'36', null);
INSERT INTO [TstGroupWeight] VALUES (N'14', N'36', null);
INSERT INTO [TstGroupWeight] VALUES (N'15', N'5', null);
INSERT INTO [TstGroupWeight] VALUES (N'16', N'37', null);
INSERT INTO [TstGroupWeight] VALUES (N'17', N'26', null);
INSERT INTO [TstGroupWeight] VALUES (N'18', N'12', null);
INSERT INTO [TstGroupWeight] VALUES (N'19', N'21', null);
INSERT INTO [TstGroupWeight] VALUES (N'20', N'10', null);
INSERT INTO [TstGroupWeight] VALUES (N'21', N'6', null);
INSERT INTO [TstGroupWeight] VALUES (N'22', N'27', null);
INSERT INTO [TstGroupWeight] VALUES (N'23', N'11', null);
INSERT INTO [TstGroupWeight] VALUES (N'24', N'5', null);
INSERT INTO [TstGroupWeight] VALUES (N'25', N'15', null);
INSERT INTO [TstGroupWeight] VALUES (N'26', N'29', null);
INSERT INTO [TstGroupWeight] VALUES (N'27', N'42', null);
INSERT INTO [TstGroupWeight] VALUES (N'28', N'11', null);
INSERT INTO [TstGroupWeight] VALUES (N'29', N'27', null);
INSERT INTO [TstGroupWeight] VALUES (N'30', N'34', null);
INSERT INTO [TstGroupWeight] VALUES (N'31', N'33', null);
INSERT INTO [TstGroupWeight] VALUES (N'32', N'11', null);
INSERT INTO [TstGroupWeight] VALUES (N'33', N'40', null);
INSERT INTO [TstGroupWeight] VALUES (N'34', N'29', null);
INSERT INTO [TstGroupWeight] VALUES (N'35', N'15', null);
INSERT INTO [TstGroupWeight] VALUES (N'36', N'9', null);
INSERT INTO [TstGroupWeight] VALUES (N'37', N'28', null);
INSERT INTO [TstGroupWeight] VALUES (N'38', N'19', null);
INSERT INTO [TstGroupWeight] VALUES (N'39', N'12', null);
INSERT INTO [TstGroupWeight] VALUES (N'40', N'27', null);
INSERT INTO [TstGroupWeight] VALUES (N'41', N'13', null);
INSERT INTO [TstGroupWeight] VALUES (N'42', N'22', null);
INSERT INTO [TstGroupWeight] VALUES (N'43', N'7', null);
INSERT INTO [TstGroupWeight] VALUES (N'44', N'8', null);
INSERT INTO [TstGroupWeight] VALUES (N'45', N'34', null);
INSERT INTO [TstGroupWeight] VALUES (N'46', N'9', null);
INSERT INTO [TstGroupWeight] VALUES (N'47', N'19', null);
INSERT INTO [TstGroupWeight] VALUES (N'48', N'21', null);
INSERT INTO [TstGroupWeight] VALUES (N'49', N'41', null);
INSERT INTO [TstGroupWeight] VALUES (N'50', N'32', null);
INSERT INTO [TstGroupWeight] VALUES (N'51', N'34', null);
INSERT INTO [TstGroupWeight] VALUES (N'52', N'28', null);
INSERT INTO [TstGroupWeight] VALUES (N'53', N'41', null);
INSERT INTO [TstGroupWeight] VALUES (N'54', N'18', null);
INSERT INTO [TstGroupWeight] VALUES (N'55', N'33', null);
INSERT INTO [TstGroupWeight] VALUES (N'56', N'40', null);
INSERT INTO [TstGroupWeight] VALUES (N'57', N'19', null);
INSERT INTO [TstGroupWeight] VALUES (N'58', N'42', null);
INSERT INTO [TstGroupWeight] VALUES (N'59', N'12', null);
INSERT INTO [TstGroupWeight] VALUES (N'60', N'4', null);
INSERT INTO [TstGroupWeight] VALUES (N'61', N'38', null);
INSERT INTO [TstGroupWeight] VALUES (N'62', N'5', null);
INSERT INTO [TstGroupWeight] VALUES (N'63', N'37', null);
INSERT INTO [TstGroupWeight] VALUES (N'64', N'36', null);
INSERT INTO [TstGroupWeight] VALUES (N'65', N'13', null);
INSERT INTO [TstGroupWeight] VALUES (N'66', N'15', null);
INSERT INTO [TstGroupWeight] VALUES (N'67', N'5', null);
INSERT INTO [TstGroupWeight] VALUES (N'68', N'4', null);
INSERT INTO [TstGroupWeight] VALUES (N'69', N'4', null);
INSERT INTO [TstGroupWeight] VALUES (N'70', N'7', null);
INSERT INTO [TstGroupWeight] VALUES (N'71', N'1', null);
INSERT INTO [TstGroupWeight] VALUES (N'72', N'21', null);
INSERT INTO [TstGroupWeight] VALUES (N'73', N'13', null);
INSERT INTO [TstGroupWeight] VALUES (N'74', N'22', null);
INSERT INTO [TstGroupWeight] VALUES (N'75', N'20', null);
INSERT INTO [TstGroupWeight] VALUES (N'76', N'1', null);
INSERT INTO [TstGroupWeight] VALUES (N'77', N'38', null);
INSERT INTO [TstGroupWeight] VALUES (N'78', N'39', null);
INSERT INTO [TstGroupWeight] VALUES (N'79', N'5', null);
INSERT INTO [TstGroupWeight] VALUES (N'80', N'33', null);
INSERT INTO [TstGroupWeight] VALUES (N'81', N'0', null);
INSERT INTO [TstGroupWeight] VALUES (N'82', N'9', null);
INSERT INTO [TstGroupWeight] VALUES (N'83', N'24', null);
INSERT INTO [TstGroupWeight] VALUES (N'84', N'36', null);
INSERT INTO [TstGroupWeight] VALUES (N'85', N'29', null);
INSERT INTO [TstGroupWeight] VALUES (N'86', N'26', null);
INSERT INTO [TstGroupWeight] VALUES (N'87', N'6', null);
INSERT INTO [TstGroupWeight] VALUES (N'88', N'37', null);
INSERT INTO [TstGroupWeight] VALUES (N'89', N'37', null);
INSERT INTO [TstGroupWeight] VALUES (N'90', N'19', null);
INSERT INTO [TstGroupWeight] VALUES (N'91', N'4', null);
INSERT INTO [TstGroupWeight] VALUES (N'92', N'25', null);
INSERT INTO [TstGroupWeight] VALUES (N'93', N'5', null);
INSERT INTO [TstGroupWeight] VALUES (N'94', N'5', null);
INSERT INTO [TstGroupWeight] VALUES (N'95', N'28', null);
INSERT INTO [TstGroupWeight] VALUES (N'96', N'16', null);
INSERT INTO [TstGroupWeight] VALUES (N'97', N'2', null);
INSERT INTO [TstGroupWeight] VALUES (N'98', N'11', null);
INSERT INTO [TstGroupWeight] VALUES (N'99', N'25', null);
INSERT INTO [TstGroupWeight] VALUES (N'100', N'14', null);
GO
INSERT INTO [TstGroupWeight] VALUES (N'101', N'14', null);
INSERT INTO [TstGroupWeight] VALUES (N'102', N'2', null);
INSERT INTO [TstGroupWeight] VALUES (N'103', N'5', null);
INSERT INTO [TstGroupWeight] VALUES (N'104', N'12', null);
INSERT INTO [TstGroupWeight] VALUES (N'105', N'35', null);
INSERT INTO [TstGroupWeight] VALUES (N'106', N'37', null);
INSERT INTO [TstGroupWeight] VALUES (N'107', N'0', null);
INSERT INTO [TstGroupWeight] VALUES (N'108', N'21', null);
INSERT INTO [TstGroupWeight] VALUES (N'109', N'38', null);
INSERT INTO [TstGroupWeight] VALUES (N'110', N'39', null);
INSERT INTO [TstGroupWeight] VALUES (N'111', N'11', null);
INSERT INTO [TstGroupWeight] VALUES (N'112', N'38', null);
INSERT INTO [TstGroupWeight] VALUES (N'113', N'25', null);
INSERT INTO [TstGroupWeight] VALUES (N'114', N'5', null);
INSERT INTO [TstGroupWeight] VALUES (N'115', N'3', null);
INSERT INTO [TstGroupWeight] VALUES (N'116', N'26', null);
INSERT INTO [TstGroupWeight] VALUES (N'117', N'38', null);
INSERT INTO [TstGroupWeight] VALUES (N'118', N'3', null);
INSERT INTO [TstGroupWeight] VALUES (N'119', N'15', null);
INSERT INTO [TstGroupWeight] VALUES (N'120', N'0', null);
INSERT INTO [TstGroupWeight] VALUES (N'121', N'14', null);
INSERT INTO [TstGroupWeight] VALUES (N'122', N'2', null);
INSERT INTO [TstGroupWeight] VALUES (N'123', N'6', null);
INSERT INTO [TstGroupWeight] VALUES (N'124', N'6', null);
INSERT INTO [TstGroupWeight] VALUES (N'125', N'14', null);
INSERT INTO [TstGroupWeight] VALUES (N'126', N'34', null);
INSERT INTO [TstGroupWeight] VALUES (N'127', N'21', null);
INSERT INTO [TstGroupWeight] VALUES (N'128', N'21', null);
INSERT INTO [TstGroupWeight] VALUES (N'129', N'31', null);
INSERT INTO [TstGroupWeight] VALUES (N'130', N'26', null);
INSERT INTO [TstGroupWeight] VALUES (N'131', N'2', null);
INSERT INTO [TstGroupWeight] VALUES (N'132', N'38', null);
INSERT INTO [TstGroupWeight] VALUES (N'133', N'40', null);
INSERT INTO [TstGroupWeight] VALUES (N'134', N'30', null);
INSERT INTO [TstGroupWeight] VALUES (N'135', N'13', null);
INSERT INTO [TstGroupWeight] VALUES (N'136', N'12', null);
INSERT INTO [TstGroupWeight] VALUES (N'137', N'14', null);
INSERT INTO [TstGroupWeight] VALUES (N'138', N'31', null);
INSERT INTO [TstGroupWeight] VALUES (N'139', N'27', null);
INSERT INTO [TstGroupWeight] VALUES (N'140', N'9', null);
INSERT INTO [TstGroupWeight] VALUES (N'141', N'21', null);
INSERT INTO [TstGroupWeight] VALUES (N'142', N'10', null);
INSERT INTO [TstGroupWeight] VALUES (N'143', N'36', null);
INSERT INTO [TstGroupWeight] VALUES (N'144', N'32', null);
INSERT INTO [TstGroupWeight] VALUES (N'145', N'38', null);
INSERT INTO [TstGroupWeight] VALUES (N'146', N'14', null);
INSERT INTO [TstGroupWeight] VALUES (N'147', N'2', null);
INSERT INTO [TstGroupWeight] VALUES (N'148', N'17', null);
INSERT INTO [TstGroupWeight] VALUES (N'149', N'34', null);
INSERT INTO [TstGroupWeight] VALUES (N'150', N'41', null);
INSERT INTO [TstGroupWeight] VALUES (N'151', N'35', null);
INSERT INTO [TstGroupWeight] VALUES (N'152', N'12', null);
INSERT INTO [TstGroupWeight] VALUES (N'153', N'23', null);
INSERT INTO [TstGroupWeight] VALUES (N'154', N'28', null);
INSERT INTO [TstGroupWeight] VALUES (N'155', N'32', null);
INSERT INTO [TstGroupWeight] VALUES (N'156', N'25', null);
INSERT INTO [TstGroupWeight] VALUES (N'157', N'17', null);
INSERT INTO [TstGroupWeight] VALUES (N'158', N'1', null);
INSERT INTO [TstGroupWeight] VALUES (N'159', N'42', null);
INSERT INTO [TstGroupWeight] VALUES (N'160', N'38', null);
INSERT INTO [TstGroupWeight] VALUES (N'161', N'29', null);
INSERT INTO [TstGroupWeight] VALUES (N'162', N'23', null);
INSERT INTO [TstGroupWeight] VALUES (N'163', N'6', null);
INSERT INTO [TstGroupWeight] VALUES (N'164', N'32', null);
INSERT INTO [TstGroupWeight] VALUES (N'165', N'14', null);
INSERT INTO [TstGroupWeight] VALUES (N'166', N'27', null);
INSERT INTO [TstGroupWeight] VALUES (N'167', N'4', null);
INSERT INTO [TstGroupWeight] VALUES (N'168', N'29', null);
INSERT INTO [TstGroupWeight] VALUES (N'169', N'16', null);
INSERT INTO [TstGroupWeight] VALUES (N'170', N'20', null);
INSERT INTO [TstGroupWeight] VALUES (N'171', N'35', null);
INSERT INTO [TstGroupWeight] VALUES (N'172', N'2', null);
INSERT INTO [TstGroupWeight] VALUES (N'173', N'37', null);
INSERT INTO [TstGroupWeight] VALUES (N'174', N'26', null);
INSERT INTO [TstGroupWeight] VALUES (N'175', N'22', null);
INSERT INTO [TstGroupWeight] VALUES (N'176', N'34', null);
INSERT INTO [TstGroupWeight] VALUES (N'177', N'0', null);
INSERT INTO [TstGroupWeight] VALUES (N'178', N'30', null);
INSERT INTO [TstGroupWeight] VALUES (N'179', N'41', null);
INSERT INTO [TstGroupWeight] VALUES (N'180', N'30', null);
INSERT INTO [TstGroupWeight] VALUES (N'181', N'41', null);
INSERT INTO [TstGroupWeight] VALUES (N'182', N'31', null);
INSERT INTO [TstGroupWeight] VALUES (N'183', N'13', null);
INSERT INTO [TstGroupWeight] VALUES (N'184', N'42', null);
INSERT INTO [TstGroupWeight] VALUES (N'185', N'41', null);
INSERT INTO [TstGroupWeight] VALUES (N'186', N'37', null);
INSERT INTO [TstGroupWeight] VALUES (N'187', N'29', null);
INSERT INTO [TstGroupWeight] VALUES (N'188', N'7', null);
INSERT INTO [TstGroupWeight] VALUES (N'189', N'5', null);
INSERT INTO [TstGroupWeight] VALUES (N'190', N'9', null);
INSERT INTO [TstGroupWeight] VALUES (N'191', N'18', null);
INSERT INTO [TstGroupWeight] VALUES (N'192', N'16', null);
INSERT INTO [TstGroupWeight] VALUES (N'193', N'29', null);
INSERT INTO [TstGroupWeight] VALUES (N'194', N'16', null);
INSERT INTO [TstGroupWeight] VALUES (N'195', N'18', null);
INSERT INTO [TstGroupWeight] VALUES (N'196', N'18', null);
INSERT INTO [TstGroupWeight] VALUES (N'197', N'14', null);
INSERT INTO [TstGroupWeight] VALUES (N'198', N'0', null);
INSERT INTO [TstGroupWeight] VALUES (N'199', N'35', null);
INSERT INTO [TstGroupWeight] VALUES (N'200', N'20', null);
GO
INSERT INTO [TstGroupWeight] VALUES (N'201', N'34', null);
INSERT INTO [TstGroupWeight] VALUES (N'202', N'35', null);
INSERT INTO [TstGroupWeight] VALUES (N'203', N'6', null);
INSERT INTO [TstGroupWeight] VALUES (N'204', N'42', null);
INSERT INTO [TstGroupWeight] VALUES (N'205', N'18', null);
INSERT INTO [TstGroupWeight] VALUES (N'206', N'31', null);
INSERT INTO [TstGroupWeight] VALUES (N'207', N'4', null);
INSERT INTO [TstGroupWeight] VALUES (N'208', N'9', null);
INSERT INTO [TstGroupWeight] VALUES (N'209', N'18', null);
INSERT INTO [TstGroupWeight] VALUES (N'210', N'14', null);
INSERT INTO [TstGroupWeight] VALUES (N'211', N'28', null);
INSERT INTO [TstGroupWeight] VALUES (N'212', N'38', null);
INSERT INTO [TstGroupWeight] VALUES (N'213', N'38', null);
INSERT INTO [TstGroupWeight] VALUES (N'214', N'7', null);
INSERT INTO [TstGroupWeight] VALUES (N'215', N'40', null);
INSERT INTO [TstGroupWeight] VALUES (N'216', N'34', null);
INSERT INTO [TstGroupWeight] VALUES (N'217', N'7', null);
INSERT INTO [TstGroupWeight] VALUES (N'218', N'15', null);
INSERT INTO [TstGroupWeight] VALUES (N'219', N'27', null);
INSERT INTO [TstGroupWeight] VALUES (N'220', N'35', null);
INSERT INTO [TstGroupWeight] VALUES (N'221', N'13', null);
INSERT INTO [TstGroupWeight] VALUES (N'222', N'41', null);
INSERT INTO [TstGroupWeight] VALUES (N'223', N'41', null);
INSERT INTO [TstGroupWeight] VALUES (N'224', N'2', null);
INSERT INTO [TstGroupWeight] VALUES (N'225', N'23', null);
INSERT INTO [TstGroupWeight] VALUES (N'226', N'18', null);
INSERT INTO [TstGroupWeight] VALUES (N'227', N'39', null);
INSERT INTO [TstGroupWeight] VALUES (N'228', N'28', null);
INSERT INTO [TstGroupWeight] VALUES (N'229', N'27', null);
INSERT INTO [TstGroupWeight] VALUES (N'230', N'8', null);
INSERT INTO [TstGroupWeight] VALUES (N'231', N'26', null);
INSERT INTO [TstGroupWeight] VALUES (N'232', N'11', null);
INSERT INTO [TstGroupWeight] VALUES (N'233', N'23', null);
INSERT INTO [TstGroupWeight] VALUES (N'234', N'13', null);
INSERT INTO [TstGroupWeight] VALUES (N'235', N'40', null);
INSERT INTO [TstGroupWeight] VALUES (N'236', N'0', null);
INSERT INTO [TstGroupWeight] VALUES (N'237', N'24', null);
INSERT INTO [TstGroupWeight] VALUES (N'238', N'24', null);
INSERT INTO [TstGroupWeight] VALUES (N'239', N'22', null);
INSERT INTO [TstGroupWeight] VALUES (N'240', N'20', null);
INSERT INTO [TstGroupWeight] VALUES (N'241', N'39', null);
INSERT INTO [TstGroupWeight] VALUES (N'242', N'2', null);
INSERT INTO [TstGroupWeight] VALUES (N'243', N'42', null);
INSERT INTO [TstGroupWeight] VALUES (N'244', N'7', null);
INSERT INTO [TstGroupWeight] VALUES (N'245', N'27', null);
INSERT INTO [TstGroupWeight] VALUES (N'246', N'8', null);
INSERT INTO [TstGroupWeight] VALUES (N'247', N'7', null);
INSERT INTO [TstGroupWeight] VALUES (N'248', N'19', null);
INSERT INTO [TstGroupWeight] VALUES (N'249', N'21', null);
INSERT INTO [TstGroupWeight] VALUES (N'250', N'35', null);
INSERT INTO [TstGroupWeight] VALUES (N'251', N'11', null);
INSERT INTO [TstGroupWeight] VALUES (N'252', N'30', null);
INSERT INTO [TstGroupWeight] VALUES (N'253', N'19', null);
INSERT INTO [TstGroupWeight] VALUES (N'254', N'18', null);
INSERT INTO [TstGroupWeight] VALUES (N'255', N'14', null);
INSERT INTO [TstGroupWeight] VALUES (N'256', N'28', null);
INSERT INTO [TstGroupWeight] VALUES (N'257', N'9', null);
INSERT INTO [TstGroupWeight] VALUES (N'258', N'4', null);
INSERT INTO [TstGroupWeight] VALUES (N'259', N'10', null);
INSERT INTO [TstGroupWeight] VALUES (N'260', N'26', null);
INSERT INTO [TstGroupWeight] VALUES (N'261', N'12', null);
INSERT INTO [TstGroupWeight] VALUES (N'262', N'0', null);
INSERT INTO [TstGroupWeight] VALUES (N'263', N'39', null);
INSERT INTO [TstGroupWeight] VALUES (N'264', N'35', null);
INSERT INTO [TstGroupWeight] VALUES (N'265', N'12', null);
INSERT INTO [TstGroupWeight] VALUES (N'266', N'6', null);
INSERT INTO [TstGroupWeight] VALUES (N'267', N'2', null);
INSERT INTO [TstGroupWeight] VALUES (N'268', N'24', null);
INSERT INTO [TstGroupWeight] VALUES (N'269', N'34', null);
INSERT INTO [TstGroupWeight] VALUES (N'270', N'10', null);
INSERT INTO [TstGroupWeight] VALUES (N'271', N'13', null);
INSERT INTO [TstGroupWeight] VALUES (N'272', N'16', null);
INSERT INTO [TstGroupWeight] VALUES (N'273', N'16', null);
INSERT INTO [TstGroupWeight] VALUES (N'274', N'3', null);
INSERT INTO [TstGroupWeight] VALUES (N'275', N'3', null);
INSERT INTO [TstGroupWeight] VALUES (N'276', N'26', null);
INSERT INTO [TstGroupWeight] VALUES (N'277', N'4', null);
INSERT INTO [TstGroupWeight] VALUES (N'278', N'23', null);
INSERT INTO [TstGroupWeight] VALUES (N'279', N'4', null);
INSERT INTO [TstGroupWeight] VALUES (N'280', N'34', null);
INSERT INTO [TstGroupWeight] VALUES (N'281', N'23', null);
INSERT INTO [TstGroupWeight] VALUES (N'282', N'18', null);
INSERT INTO [TstGroupWeight] VALUES (N'283', N'17', null);
INSERT INTO [TstGroupWeight] VALUES (N'284', N'25', null);
INSERT INTO [TstGroupWeight] VALUES (N'285', N'32', null);
INSERT INTO [TstGroupWeight] VALUES (N'286', N'24', null);
INSERT INTO [TstGroupWeight] VALUES (N'287', N'37', null);
INSERT INTO [TstGroupWeight] VALUES (N'288', N'38', null);
INSERT INTO [TstGroupWeight] VALUES (N'289', N'18', null);
INSERT INTO [TstGroupWeight] VALUES (N'290', N'40', null);
INSERT INTO [TstGroupWeight] VALUES (N'291', N'12', null);
INSERT INTO [TstGroupWeight] VALUES (N'292', N'9', null);
INSERT INTO [TstGroupWeight] VALUES (N'293', N'9', null);
INSERT INTO [TstGroupWeight] VALUES (N'294', N'32', null);
INSERT INTO [TstGroupWeight] VALUES (N'295', N'11', null);
INSERT INTO [TstGroupWeight] VALUES (N'296', N'23', null);
INSERT INTO [TstGroupWeight] VALUES (N'297', N'25', null);
INSERT INTO [TstGroupWeight] VALUES (N'298', N'30', null);
INSERT INTO [TstGroupWeight] VALUES (N'299', N'24', null);
INSERT INTO [TstGroupWeight] VALUES (N'300', N'6', null);
GO
COMMIT TRANSACTION
GO
答案 0 :(得分:0)
这是一个bin-packing问题,对于SQL来说,一般的解决方案可能太难了。在实践中非常有效的一种方法是枚举组,然后为每个组取第n个值。所以,如果你想要三个小组:
select t.*, (seqnum % 3) as grp
from (select t.*,
row_number() over (order by itemvalue) as seqnum
from t
) t;
当值在一个较小的相当密集的范围内(以及在其他情况下)时,这是有效的。当存在少量异常值时,它不起作用。
答案 1 :(得分:0)
假设您要根据ItemID
排序的数据创建这些组,NTILE
失败,因为它会创建具有相同行数但不是相同总和的组。您只需要根据值的累积和应用分位数逻辑。
计算 n 分位数非常类似于:
n * (RANK() OVER (ORDER BY ItemID) - 1)
/ COUNT(*) OVER()
现在将计数更改为SUM
:
(n * sum(ItemValue) over (order by ItemID rows unbounded preceding) - 1)
/ sum(ItemValue) over ()
SQL Server 2008不支持累积和,因此您需要使用相关子查询(性能可能不好)。
with cte as
(
select ItemID, ItemValue,
-- sum of all values
sum(ItemValue) over () as all_sum,
-- cumulative sum
(select sum(ItemValue)
from TstGroupWeight as t2
where t2.ItemID <= t1.ItemID) as cum_sum
-- much easier in SQL Server 2012
-- sum(ItemValue)
-- over (order by ItemID
-- rows unbounded preceding) as cum_sum
from TstGroupWeight as t1
)
select ItemID, ItemValue, cum_sum,
-- n = number of quantiles
(n * cum_sum-1) / all_sum as grp
from cte
请参阅fiddle
当然可以使用递归(或游标)应用相同的逻辑,但是根据实际行数,所有版本可能运行缓慢。