Search code examples
sqlpostgresqlunpivot

Converting the header row to a column PostgreSQL


I am new to PostgreSQL. I have a table with three columns as mentioned below and i want to convert the header row to a column with the corresponding values in another column and finally a new column that checks if the value is missing or not.

ID   NAME   EMAIL            SALARY
1    John   ''               1000
2    Sam    [email protected]   6000
3    Tom    [email protected]   9000
4    Bob    [email protected]    500
5    Lari   [email protected]   3000

The expected output for each ID:

ID   fields      Value            Verified
 1    NAME        John              Yes
      EMAIL        ''               No
      SALARY      1000              Yes

 2    NAME        Sam               Yes
      EMAIL      [email protected]     Yes
      SALARY      6000              Yes

Solution

  • What you call the header row is no row at all, but just the column names. You want one row per ID and column. While this is something I'd rather do in the app and not in SQL, it is possible of course. You need one query per column. Glue their results together with UNION ALL.

    select
      id, 
      'NAME' as fields,
      name as value,
      case when name is null or name = '' then 'No' else 'Yes' as verified
    from mytable
    union all
    select
      id, 
      'EMAIL' as fields,
      email as value,
      case when email is null or email = '' then 'No' else 'Yes' as verified
    from mytable
    union all
    select
      id, 
      'SALARY' as fields,
      cast(salary as varchar) as value,
      case when salary is null then 'No' else 'Yes' as verified
    from mytable
    order by id, fields;
    

    But as mentioned, it would be better just to select * from mytable and care about the presentation of the data in your app. This is probably much faster, too.