简体   繁体   中英

Use LISTAGG to select multiple rows on joined table

Assuming I have the following two tables Table 1 - User

ID UserName
001 abc
002 bcd
003 def

TAble 2 - Values

ID Tag Price
001 start 1
001 middle 2
001 end 3
002 start 3
002 end 4
003 start 1
003 middle 2
003 end 3

I'm interested with values tagged with "start" and "end", ie, the expected result is:

001 abc 1, 2
002 bcd 3, 4
003 def 1, 3

Note: I need the UserName in table 1.

Thanks so much.

Are you looking for something like this?

select u.id, u.name, listagg(price, ', ') within group (order by price) as prices
from user u left outer join
     values v
     on u.id = v.id and v.tag in ('start', 'end')
group by u.id, u.name;

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