且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将列的不同值转换为行postgres

更新时间:2023-10-25 16:00:04

您可以使用条件聚合:

select ad_id,
       max(case when name = 'name' then valueofname end) as name,
       max(case when name = 'age' then valueofname end) as age,
       max(case when name = 'birthday' then valueofname end) as birthday,
       max(case when name = 'job' then valueofname end) as job
from t
group by ad_id;

在SQL Server中,您也可以使用pivot做类似的事情.

In SQL Server, you can also do something similar with pivot.