[英]How to compare date between years that stored in one column in Postgresql?
I have this output:我有这个输出:
It's data of stock symbol, closing values and the year of the closing of the stock.它是股票代码、收盘价和股票收盘年份的数据。 I make the output from this query:我从这个查询中输出:
select symbol, close, extract(year from time) as tahun
from (select * from (select * from tabel1 where close > (select avg(close) from tabel1))
as result where open < close) as result group by symbol, close, tahun
Now, I want to find the 2016 closing values that has higher value than year 2017 (2016 > 2017).现在,我想找到价值高于 2017 年(2016 > 2017)的 2016 年收盘价。 I'm stuck, please anyone can help?我被卡住了,请问有人可以帮忙吗?
[EDIT] [编辑]
This is the original data:这是原始数据:
How I define closing value for a year:我如何定义一年的收盘价:
select * from tabel1 where close > (select avg(close) from tabel1)
select * from (select * from tabel1 where close > (select avg(close) from tabel1)) as result where open < close
select symbol, close, extract(year from time) as tahun
from (select * from (select * from tabel1 where close > (select avg(close) from tabel1)) as result where open < close) as result group by symbol, close, tahun
You can use window functions with filtering:您可以使用带过滤的窗口函数:
select t.*
from (select t.*,
max(close) filter (where tahun = 2016) over (partition by symbol) as close_2016
from table1 t
) t
where tahun = 2017 and close < close_2016;
Or, if you prefer, a correlated subquery:或者,如果您愿意,可以使用相关子查询:
select t.*
from table1 t
where t.tahun = 2017 and
t.close < (select tt.close from table1 tt where tt.symbol = t.symbol and tt.tahun = 2016);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.