Return column name and distinct values

前端 未结 3 2041
生来不讨喜
生来不讨喜 2021-01-22 02:09

Say I have a simple table in postgres as the following:

+--------+--------+----------+
|  Car   |  Pet   |   Name   |
+--------+--------+----------+
| BMW    |           


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-22 03:02

    With JSON functions row_to_json() and json_each_text() you can do it not specifying number and names of columns:

    select distinct key as col, value as vals
    from (
        select row_to_json(t) r
        from a_table t
        ) t,
        json_each_text(r)
    order by 1, 2;
    

    SqlFiddle.

提交回复
热议问题