[英]Transform row wise postgres data to grouped column wise data
I have a stock market data which looks likes like 我有一个类似的股市数据
instrument_symbol|close |timestamp |
-----------------|------|-------------------|
IOC |134.15|2019-08-05 00:00:00|
YESBANK | 83.75|2019-08-05 00:00:00|
IOC |135.25|2019-08-02 00:00:00|
YESBANK | 88.3|2019-08-02 00:00:00|
IOC |136.95|2019-08-01 00:00:00|
YESBANK | 88.4|2019-08-01 00:00:00|
IOC | 139.3|2019-07-31 00:00:00|
YESBANK | 91.2|2019-07-31 00:00:00|
YESBANK | 86.05|2019-07-30 00:00:00|
IOC | 133.5|2019-07-30 00:00:00|
IOC |138.25|2019-07-29 00:00:00|
I want to transform it to 我想将其转换为
timestamp, IOC, YESBANK
2019-08-05 00:00:00 134.15 83.75
2019-08-02 00:00:00 135.25 88.3
......
.....
...
format. 格式。
Is there some Postgres query to do this? 是否有一些Postgres查询可以做到这一点? Or do we have to do this programmatically?
还是我们必须以编程方式执行此操作?
You can use conditional aggregation. 您可以使用条件聚合。 In Postgres, I like the
filter
syntax: 在Postgres中,我喜欢
filter
语法:
select "timestamp",
max(close) filter (where instrument_symbol = 'IOC') as ioc,
max(close) filter (where instrument_symbol = 'YESBANK') as yesbank
from t
group by "timestamp"
order by 1 desc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.