0

I have postgres table with structure:

|key| position |    date    |
|---|----------|------------|
| 1 |     5    | 2017-07-01 |
|---|----------|------------|
| 1 |     9    | 2017-07-02 |
|---|----------|------------|
| 2 |     4    | 2017-07-01 |
|---|----------|------------|
| 2 |     8    | 2017-07-02 |

But I need to have the selected data in a format like this:

| key | 2017-07-01 | 2017-07-02 |
|-----|------------|------------|
|  1  |      5     |      9     |
|-----|------------|------------|
|  2  |      4     |      8     |

How can I do something like this?

Steve Okay
  • 80
  • 12

1 Answers1

1

If you have one row per key and per date, then one way is conditional aggregation

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
Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236