Get the App
SLTechnology News&Howtos  ›  Database  › 

Add three rows, if the same value in a column

Shulou Source: shulou.com Published: 2022-06-01 07:02:11 10月03日 Update

There is an Postgres database and tables with three columns. The data structure is in the external system, so I can't modify it.

Each object consists of three rows (the same value of the listed element_id-- row represents the same object in this column), for example:

Key value element_id--status active 1name exampleNameAAA 1city exampleCityAAA 1status inactive 2name exampleNameBBB 2city exampleCityBBB 2status inactive 3name exampleNameCCC 3city exampleCityCCC 3

I want all the values to describe each object (name, status, and city).

The output for this example should be:

ExampleNameAAA | active | exampleCityAAAexampleNameBBB | inactive | exampleCityBBBexampleNameCCC | inactive | exampleCityCCC

I know how to add two lines:

Select a.value as name, b.value as statusfrom the_table a join the_table b on a.element_id = b.element_id and b. "key" = 'status'where a. "key" =' name'

How is it possible to join three columns?

Tags: Object data same three lines three column representative name city database data structure state example system structure a.memb. output Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker macOS Linux NVidia