2017-11-02 2 views
3

Ich habe eine Tabelle:Postgres: konvertieren einzelne Zeile mehrere Zeilen

Table_Name: price_list 
--------------------------------------------------- 
| id | price_type_a | price_type_b | price_type_c | 
--------------------------------------------------- 
| 1 | 1234  |  5678  |  9012  | 
| 2 | 3456  |  7890  |  1234  | 
| 3 | 5678  |  9012  |  3456  | 
--------------------------------------------------- 

Ich brauche eine Auswahlabfrage in Postgres, die wie folgt zur Folge ergibt:

--------------------------- 
| id | price_type | price | 
--------------------------- 
| 1 | type_a | 1234 | 
| 1 | type_b | 5678 | 
| 1 | type_c | 9012 | 
| 2 | type_a | 3456 | 
| 2 | type_b | 7890 | 
| 2 | type_c | 1234 | 
... 

Jede Hilfe mit Links zu ähnlichen Beispiele sehr geschätzt.

+0

nur 'select id, 'a', a union select id,‘ b'b und so weiter? .. –

+0

Danke! das funktioniert großartig .. wird es für große Datenmengen optimal sein? – skybunk

+0

nichts smarter kommt ATM sowieso :) –

Antwort

0

try smth wie:

select id, 'type_a',type_a from price_list 
union all 
select id, 'type_b',type_b from price_list 
union all 
select id, 'type_c',type_c from price_list 
; 

Update wie a_horse_with_no_name schon sagt, ist die Vereinigung Weg DISTINCT Werte auszuwählen, hier für UNION ALL lieber wäre - nur für den Fall (ich weiß nicht, ob id UNIQUE)

natürlich, wenn es in Großbritannien ist - es wird

+0

'Union all' wäre besser –

+0

@a_horse_with_no_name danke - aktualisiert –

Verwandte Themen