2

I have a data result from a view like below,

Current view

But I want a view like this

Need a view from the original view

Can any one help me to do this via postgresql without using extensions.

  • 1
    Please go through all the questions for [pivot or crosstab](https://stackoverflow.com/search?q=%28%5Bpivot%5D+or+%5Bcrosstab%5D%29+%5Bpostgresql%5D) –  Dec 11 '19 at 07:52
  • 1
    This is typically much better done in the application, than in SQL –  Dec 11 '19 at 07:53

1 Answers1

4

use aggregation

select project, max(case when role='owner' then name end) as owner,
 max(case when role='client' then name end) as client,
 max(case when role='Team' then name end) as Team
from table 
group by project;

Alternatively you can use the filter() clause which makes this a bit easier to read:

select project, 
       max(name) filter (where role='owner') as owner,
       max(name) filter (where role='client') as client,
       max(name) filter (where role='Team') as Team
from table 
group by project;
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63