Postgres - Transpose Rows to Columns
I have the following table, which gives multiple email addresses for each user. I need to flatten this out to columns on a user query. To give me the "newest" 3 email addresses based on the creation date. user.name | user.id | email1 | email2 | email3** Mary | 123 | mary@gmail.com | mary@yahoo.co.uk | mary@test.com Joe | 345 | joe@gmail.com | [NULL] | [NULL] Erwin Brandstetter Use crosstab() from the tablefunc module. SELECT * FROM crosstab( $$SELECT user_id, user_name, rn, email_address FROM ( SELECT u.user_id, u.user_name, e.email_address , row_number() OVER (PARTITION BY u.user_id ORDER BY