我们如何从列值中检索信息作为行信息? 例如,我们有一个表
|-----------|-------------|--------------|
| ID | Field | FieldVlaue |
|-----------|-------------|--------------|
| 1 | Name | Jack |
|-----------|-------------|--------------|
| 1 | Country | Australia |
|-----------|-------------|--------------|
| 1 | PostCode| 0277 |
|-----------|-------------|--------------|
| 2 | Name | John |
|-----------|-------------|--------------|
| 2 | address | Wyard |
|-----------|-------------|--------------|
| 2 | ContactNum | 1234567 |
|-----------|-------------|--------------|
基于上表,我们如何获得像下面的表
|-----------|-------------|--------------|-------------|---------|-----------|
| ID | Name | Country | PostCode |Address | ContactNum|
|-----------|-------------|--------------|-------------|---------|-----------|
| 1 | Jack | Australia | 0277 |Null | Null |
|-----------|-------------|--------------|-------------|---------|-----------|
| 2 | John | Null | Null | Wyard |1234567
|-----------|-------------|--------------|-------------|---------|-----------|
答案 0 :(得分:2)
使用条件汇总
select id,
max(case when Field='Name' then FieldVlaue end) as Name,
max(case when Field='Country' then FieldVlaue end) as Country,
max(case when Field='PostCode' then FieldVlaue end) as PostCode,
max(case when Field='Address' then FieldVlaue end ) as Address,
max(case when Field='ContactNum' then FieldVlaue end) as ContactNum
from t
group by id