sql—如何从第一个表中重复获得第二个表的行数值

bn31dyow  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(319)

属性

id  value
1   asd
2   def
3   efg
4   jij

佣金

id  comm
2   20
3   30

我希望结果是

value,  comm
asd     20
def     30
efg     20
jij     30

第二个表值根据第一个表记录重复。

yx2lnoni

yx2lnoni1#

如果你只想重复,用 row_number() 和算术:

select c.*, a.*
from (select a.*, row_number() over (order by id) as seqnum, count(*) over () as cnt
      from attributes a
     ) a join
     (select c.*, row_number() over (order by id) as seqnum
      from commission c
     ) c
     on a.seqnum - 1 = mod(c.seqnum, a.cnt);

相关问题