gpt4 book ai didi

sql - 每个项目选择 X 个随机用户

转载 作者:行者123 更新时间:2023-12-03 02:46:42 25 4
gpt4 key购买 nike

我有 2 个表:ItemsUsers

Items 包含 ItemID 和 Num_Users
Users 包含 UserID

对于每个 ItemID,我需要根据 Num_Users 列中指定的值随机选择一些用户。

我在 SQL Fiddle 上创建了一组样本数据.

最佳答案

一种方式。 SQL Fiddle

SELECT Items.[ItemID],
UserID
FROM Items
OUTER APPLY (SELECT TOP ([Num_Users]) *
FROM Users
ORDER BY Newid()) A

您还可以使用 CRYPT_GEN_RANDOM(4) 代替 Newid() 以获得更大程度的随机性,但有 some issues with this在非最新版本的 SQL Server 2008 上。

关于sql - 每个项目选择 X 个随机用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12883303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com