Sql Postgre将值转换为列

Sql Postgre将值转换为列,sql,postgresql,Sql,Postgresql,我有一个结构如下的postgres表: |key| position | date | |---|----------|------------| | 1 | 5 | 2017-07-01 | |---|----------|------------| | 1 | 9 | 2017-07-02 | |---|----------|------------| | 2 | 4 | 2017-07-01 | |---|----------|----

我有一个结构如下的postgres表:

|key| position | date | |---|----------|------------| | 1 | 5 | 2017-07-01 | |---|----------|------------| | 1 | 9 | 2017-07-02 | |---|----------|------------| | 2 | 4 | 2017-07-01 | |---|----------|------------| | 2 | 8 | 2017-07-02 | |钥匙|位置|日期| |---|----------|------------| | 1 | 5 | 2017-07-01 | |---|----------|------------| | 1 | 9 | 2017-07-02 | |---|----------|------------| | 2 | 4 | 2017-07-01 | |---|----------|------------| | 2 | 8 | 2017-07-02 | 但我需要将所选数据的格式如下:

| key | 2017-07-01 | 2017-07-02 | |-----|------------|------------| | 1 | 5 | 9 | |-----|------------|------------| | 2 | 4 | 8 | |图例| 2017-07-01 | 2017-07-02| |-----|------------|------------| | 1 | 5 | 9 | |-----|------------|------------| | 2 | 4 | 8 |
我怎样才能做到这一点呢?

如果每个键和每个日期都有一行,那么一种方法就是条件聚合

select 
key,
min(case when date = '2017-07-01' then position end) as "2017-07-01",
min(case when date = '2017-07-02' then position end) as "2017-07-02"
from t
group by key

请看这里Google pivot查询可能重复的thants,这就是我需要的