用SQL选择行n次 [英] select row n times with SQL
问题描述
我有3个表。 用户,组和 PersonGroup 。
每个人可以在多个组中。
在 PersonGroup 表中有一个名为 ShowCount 的字段,表示 Person 应该显示多少次。
I have 3 tables. Person, Group and PersonGroup. Each Person can be in multiple Groups. In the PersonGroup table there's a field called ShowCount, which says how many times a Person should be shown.
示例:
人员Bill在组StackOverflow中,应显示3次,以便输出结果如下
Example: Person Bill is in group StackOverflow and should be displayed 3 times so the output should be like this
Bill
Bill
Bill
有没有办法用SQL做到这一点?
is there a way to do this with SQL?
推荐答案
Hello数字表可以帮助您的解决方案。
您可以在 http: //www.kodyaz.com/articles/sql-server-tsql-cte-numbers-table.aspx
Hello a numbers table can help you with solution. You can find the function code at http://www.kodyaz.com/articles/sql-server-tsql-cte-numbers-table.aspx
这里是一个示例t-sql脚本
Here is a sample t-sql script
select
pg.showcount,
p.name,
g.groupname
from PersonGroup pg
inner join Person p on p.personid = pg.personid
inner join Groups g on g.groupid = pg.groupid
inner join dbo.NumbersTable(1,12,1) n on pg.showcount >= n.i
我希望这有帮助,
I hope this helps,
这篇关于用SQL选择行n次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!