[英]Variable LIMIT from other SELECT
I got a table av
with the columns ID, amount
.我得到了一个表
av
,其中列ID, amount
。
Also got a users
table with the columns ID, timestamp
还得到了一个带有列
ID, timestamp
的users
表
Now each voucher relates to the first x
users (ordered by timestamp), where x
is the amount
value of the certain voucher.现在每个凭证都与
first x
用户相关(按时间戳排序),其中x
是特定凭证的amount
值。
I did a dbfiddle with a small example我用一个小例子做了一个dbfiddle
My expected result:我的预期结果:
voucherID userID
11 10
12 10
12 11
For better understanding:为了更好地理解:
An admin creates a voucher that is supposed to go to the first X users registered.管理员创建了一个凭证,该凭证应该发给注册的第一个 X 用户。 Each voucher has a different X
每张优惠券都有不同的 X
I think this is what you want:我认为这就是你想要的:
with
u as (
select userID, timestampMock,
row_number() over (order by timestampMock) as rn
from users
)
select
av.voucherID, u.userID
from av
join u on u.rn <= av.amount
order by av.voucherID, u.userID
I tested it in your dbfiddle and its result is:我在您的 dbfiddle 中对其进行了测试,结果是:
voucherID userID
--------- ------
11 10
12 10
12 11
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.