【发布时间】:2021-03-22 05:47:45
【问题描述】:
我在 postgres 中有一张桌子:
| PersonID |Description | Value |
|----------|-------------|-------|
| 1 | Name | Jane |
| 1 | Last name | Doe |
| 1 | Age | 23 |
| 1 | Country | USA |
| 2 | Name | Steve |
| 2 | Last name | Jobs |
| 2 | Age | 40 |
| 2 | Country | India |
| 1 | Height | 1.80 |
| 1 | Weight | 80 |
| 2 | Height | 1.72 |
| 2 | Weight | 79 |
我想要这个(obs:参考代码=身高+体重):
| Name | Last_name | Age | Country | Ref. code |
|---------|-----------|-----|---------|-----------|
| Jane | Doe | 23 | USA | 1.8080 |
| Steve | Jobs | 40 | India | 1.7279 |
我已经有了这个脚本,但是我没有 ref 代码列的 concat 部分:
select person_id,
max(case when description = 'Name' then value end) as name,
max(case when description = 'Last name' then value end) as last_name,
max(case when description = 'Age' then value end) as age,
max(case when description = 'Country' then value end) as country
from mytable
group by person_id
请帮忙!并提前感谢
【问题讨论】:
标签: sql string postgresql pivot aggregate-functions