I'm looking for a way to structure given tables:
TABLE: group TABLE: person
------------------------- -------------------------------------------------
| Group_id | Group_name | | Person_id | Person_name | Person_fid | Group_id |
------------------------- -------------------------------------------------
| 1 | Group44 | | 1 | John | 2 | 1 |
------------------------- -------------------------------------------------
| 2 | Best Group | | 2 | George | 1 | 1 |
------------------------- -------------------------------------------------
| 3 | Peter | 2 | 2 |
(Table person has additional columns like person_nickname, person_status etc.)
Into a Result like this: (Groupname, person_fid1 columns, person_fid2 columns)
-------------------------------------------------------------------
| Groupname | fid1_pname | fid 1_pstat | fid2_pname | fid2_pstat |
-------------------------------------------------------------------
| Group44 | George | 1 | John | 0 |
-------------------------------------------------------------------
| Best Group | NULL | NULL | Peter | 1 |
-------------------------------------------------------------------
There will be 6 different Person_fids which I need to generate columns out of.
If a Group doesn't have a person with a certain fid it can show NULL for those columns. Is there a way to do this?
I've tried using:
SELECT group.name, MAX(CASE WHEN person.Person_fid = 1 THEN |get column infos|
ELSE NULL, NULL, NULL END) 3 columns fid1,
MAX(CASE WHEN person.Person_fid = 2 THEN |get column infos|
ELSE NULL, NULL, NULL END) 3 columns fid2
But I couldn't get very far as I have no idea how to even search for this kind of stuff.
groupis a reserved word. In consequence, it's a poor choice for a table/column identifier. That said, why do you want to do this?fid 1_pstatwhich columns values? and do you want to show 6 person in single row