如何将一个 table 中的 2 列计算到新的 table 中? PostgreSQL

How do I make calculation from 2 columns in one table, into new table? Postgresql

我有一个很大的 table 叫做“postgres”,它有 3 列(日期、打开、关闭)和 65000 行。

 postgres
    
        Date       |      Open       |       Close
----------------------------------------------------------
        2019-01-01 |     1.03212     |     1.03243
        2019-01-01 |     1.06212     |     1.09243
        2019-01-02 |     1.02212     |     1.08243
        2019-01-04 |     1.08212     |     1.07243
        +65000 rows

我需要计算一下。类似 (case when Open < Close then 1 else 0 end) 的所有行 table,接下来我需要将答案放入新的 table “Zad2”。它需要看起来像:

    Zad2
    
    Type       |     Amount  
-------------------------------  
    positive   |     23232     
    negative   |     11433     
    equal      |     322        

感谢帮助,对不起我的英语)

您可以使用 case 表达式:

select (case when open < close  then 'increasing'
             when open > close  then 'decreasing'
             when open = close  then 'equal'
        end) as grp, count(*)
from t
group by grp;

我不确定“正面”和“负面”的含义,所以我添加了对我有意义的标签。

您可以使用 insert(如果 table 已经存在)或 create table as(如果 table不存在)。