从另一列中的每个唯一值从列中随机选择一个值
我一直在完成似乎很简单的任务。但是,我整天都在没有进步的情况下度过了一整天,我相信现在是时候向这个社区寻求帮助了。我只有2列的下表:
人 | 钥匙编号 |
---|---|
角色 | 11111 |
角色 | 22222 |
角色 | 33333 |
Personb | 44444 |
Personb | 55555 |
我需要随机选择与某人关联的密钥编号并将其显示在新表中。我需要与随机键号一起显示所有人员。与人相关的密钥数量为2-5。下面显示了输出表的一个示例:
人 | 钥匙编号 |
---|---|
Persona | 22222 |
Personb | 44444 |
关于如何处理此任务的任何想法?
提前致谢!
I've been working on a task that seems pretty straightforward. However, I've spent this entire day without progressing and I believe it's now time to ask this community for help. I have the following table with simply 2 columns:
Person | key numbers |
---|---|
PersonA | 11111 |
PersonA | 22222 |
PersonA | 33333 |
PersonB | 44444 |
PersonB | 55555 |
I need to randomly select a key number associated with a person and display it in a new table. I need to display all persons alongside a random key number. The number of keys associated with a person ranges from 2-5. An example of the output table is displayed below:
Person | key numbers |
---|---|
PersonA | 22222 |
PersonB | 44444 |
Any ideas on how to approach this task?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试此代码:
选择不同的人,first_value([键号]),而不是(从表格订购(按newid()划分)从表
划分Try This Code :
SELECT DISTINCT Person ,FIRST_VALUE([key numbers]) OVER (PARTITION BY Person ORDER BY NEWID()) FROM TABLE