简体   繁体   中英

How to get the 2nd table value repeated for number of rows from the first table

attributes

id  value
1   asd
2   def
3   efg
4   jij

commision

id  comm
2   20
3   30

I want the result as

value,  comm
asd     20
def     30
efg     20
jij     30

The 2nd table value repeat as per the 1st table records.

If you just want to repeat, use row_number() and arithmetic:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM