I have a database table like so:
| user_id | field_id | field_value |
|---|---|---|
| 1 | first_name | Jim |
| 1 | last_name | Bob |
| 1 | postcode | 12345 |
| 2 | first_name | Joe |
| 2 | last_name | Blogs |
| 2 | postcode | 67890 |
I need to convert it to the format below, converting the custom fields to columns and deduplicating the user ids.
| user_id | first_name | last_name | postcode |
|---|---|---|---|
| 1 | Jim | Bob | 12345 |
| 2 | Joe | Blogs | 67890 |
How can I do this in SQL and how would you describe technically what it is I'm trying to do? I can't think of the right terminology to search for a solution to this.
I have over 20 custom fields, which may change over time so a scalable solution would be nice, but I don't mind writing a manual query for for my current setup just to get it working.
Thanks!