[英]postgresql: Splitting multiple columns into rows
希望你們一切都好。
這是我的數據集的屏幕截圖 ] 1
現在真實數據集目前有近三千行,而我正在努力做的是將聯系人從十二列分解為三列。
Company name | Contact | Position | Email
FooFoo Inc | Me | ceo | stuff@
FooFoo Inc | You | ceo | stuff@
FooFoo Inc | Friend | ceo | stuff@
FooFoo Inc | Picard | cto | stuff@
Pear Co..... etc.
關於如何解決這個問題的任何建議? 謝謝大家!
使用UNION ALL
:
select company_name, primary_contact contact, contact_position position, email
union all select company_name, contact_2, contact_2_position, contact_2_email
union all select company_name, contact_3, contact_3_position, contact_3_email
union all select company_name, contact_4, contact_4_position, contact_4_email
使用橫向連接:
select t.company_name, v.*
from t cross join lateral
(values (t.contact, t.contact_position, t.email),
(t.contact_2, t.contact_2_position, t.contact_2_email),
(t.contact_3, t.contact_3_position, t.contact_3_email),
. . .
) v(contact, contact_position, email);
您可能要添加:
where v.contact is not null
但這從您的樣本數據中並不明顯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.