I am trying to build a view in Postgres that uses 3 tables worth of data. I am not sure if this is possible and have searched around a bit on google but didn't turn up anything conclusive. This is what I am attempting to do:
I have a table of item names - lets say there are 5 items:
fruits
id | name
1  | banana
2  | orange
3  | pear
4  | apple
5  | grape
I then have a list of people
people
id  |  name
1   |  Joe Blow
2   |  Sally Smith
3   |  John Jones
4   |  Sam Benny
5   |  Nick Stevens
6   |  Peter Sandwitch
7   |  Sarah Morgan
I then have a third table linking the two above:
people_fruits
person_id | fruit_id
1         | 1
1         | 2
1         | 3
1         | 4
2         | 1
2         | 3
3         | 5
6         | 3
7         | 3
7         | 4
What I am trying to do is be able to dynamically create a view utilizing the above that will change the columns based on the content of the fruit table. for example, I would want the view to show the above data as follows:
my_fruity_view
name            | bananna | orange | pear | apple | grape
Joe Blow        | X       | X      | X    | X     |
Sally Smith     | X       |        | X    |       |
John Jones      |         |        |      |       | X
Sam Benny       |         |        |      |       |
Nick Stevens    |         |        |      |       |
Peter Sandwitch |         |        | X    |       |
Sarah Morgan    |         |        | X    | X     |
Then if I was to add the fruit mango at a later time, the next time the query was run (without modification), it would add that as a column:
my_fruity_view
name            | bananna | orange | pear | apple | grape | mango
Joe Blow        | X       | X      | X    | X     |       |
Sally Smith     | X       |        | X    |       |       |
John Jones      |         |        |      |       | X     |
Sam Benny       |         |        |      |       |       |
Nick Stevens    |         |        |      |       |       |
Peter Sandwitch |         |        | X    |       |       |
Sarah Morgan    |         |        | X    | X     |       |
Is such a query possible? I see a few things like this on stack overflow - but it seems like its done on a per column basis - but my data needs to be dynamic.
I can achieve this with programming but I would much prefer to pack it up into a view to keep things neat. any help on this would be appreciated.
 
    