简体   繁体   English

如何比较存储在 Postgresql 中一列中的年份之间的日期?

[英]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:我如何定义一年的收盘价:

  1. I extract all the closing values that has higher values than averag closing values.我提取了所有比平均收盘值更高的收盘值。 Here's the query:这是查询:
select * from tabel1 where close > (select avg(close) from tabel1)
  1. Then, from query above I extract the closing values that has higher values than the opening (open < close).然后,从上面的查询中,我提取了比开盘价(开盘价 < 收盘价)更高的收盘价。 Here's the query:这是查询:
select * from (select * from tabel1 where close > (select avg(close) from tabel1)) as result where open < close
  1. From the second output, I tried to group the data by symbol and find the 2016 closing values that has higher values than 2017 closing values.从第二个输出中,我尝试按符号对数据进行分组,并找到比 2017 年收盘价更高的 2016 年收盘价。 But I'm stuck with this query, seems that I don't know how to compare between the years that stored in one column:但是我坚持使用这个查询,似乎我不知道如何比较存储在一列中的年份:
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.

相关问题 Postgresql,根据另一列添加(年,月或日)到目前为止 - Postgresql, add (years, months or days) to date based on another column PostgreSQL - 从日期列获取不同年份的计数 - PostgreSQL - get count of distinct years from date column SQL(postgreSQL)如何比较和排序忽略年份的日期 - SQL (postgreSQL) how to compare and sort dates ignoring years 如果我有一个&#39;dmY&#39;格式的日期列,如何在mysql中比较日期(在一定范围之间)? - How to compare date(between certain range) in mysql if I have one date column in 'd-m-Y' format? 如何将时间(年)添加到表中的日期列 - How to add time (years) to a date column in a table 如何比较行值和下一行之间的日期列 - How to compare Date Column between a row value and next row 如何在计算年时差时包括结束日期?(Postgresql) - How to include end date while calculating time difference in years?(Postgresql) 查找今天日期和日期列之间的年长 - Finding the length of years between todays date and a column of dates 在PostgreSQL中使用两年之间 - Between use two years in postgresql 如何在PostgreSQL中将一行中的列值与另一行中的列值进行比较 - How to compare a column value in one row to column value in another row in postgresql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM