简体   繁体   中英

Oracle consolidate 2 rows into column


Need help to display 2 rows as consolidated column
Made this image to illustrate the problem Thank you for your help 在此处输入图片说明

If you only require 2 cities then something simple might be to use analytic functions:

select distinct studentname
     , min(city) over ( partition by studentname ) as city1
     , min(street1) over ( partition by studentname ) as street1
     , case when min(city) over ( partition by studentname ) 
                  <> nvl( max(city) over ( partition by studentname ), 'x')
              then max(city) over ( partition by studentname ) end as city2
     , case when min(street) over ( partition by studentname ) 
                  <> nvl( max(street) over ( partition by studentname ), 'x')
              then max(street) over ( partition by studentname ) end as street2
  from my_table

Though I have to add that you probably shouldn't be doing this. What happens if a student has 3 addresses?

I think the way you're thinking about this is not the correct one.

You should return the 2 rows for "Ted" and manipulate the output using your view/report code. That'd be much easier and would allow you to change your view/report really fast if your requirements change.

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