select insert into语句和处理时序数据和外键怎么办?

How to do select insert into statements and handling time series data and foreign keys?

我完全不知道如何解决我遇到的问题。我试图从一个 table 生成一个布尔语句并将其插入另一个 table 的列。我能够生成布尔语句并创建一个新列,如下面的代码和输出所示:

 SELECT date_price, stock_id, (adj_close_price>ub1_50_d) as d20_l_d50
 FROM bollinger_bands
 LIMIT 100; 

这是我目前拥有的:

INSERT INTO decisions (stock_id,
                   date_price,  
                   created_date,
                   last_updated_date,
                   d20_l_d50)
 VALUES         
                   ( ,
                     ,
                   now(),
                   now(),
                   (SELECT adj_close_price > ub1_50_d as d20_l_d50
                   FROM bollinger_bands)
                   );
                   

我对如何将布尔输出插入另一个 table 的列感到困惑。我特别不清楚的两列是:日期列和 stock_id 列。我什至不知道我现在是否走在正确的道路上以及要寻找什么。感谢任何帮助和建议。

以下语法 creates a new table 使用您提供的数据命名为 decisions

select  'Dummy', now(), now(), now(),
adj_close_price > ub1_50_d as d20_l_d50
INTO decisions 
from bollinger_bands
--limit 100 -- For testing?

如果您已经有一个包含数据的 table,您将需要 update that table

UPDATE decisions dcs
SET d20_l_d50 = adj_close_price>ub1_50_d
FROM bollinger_bands bbd
WHERE bbd.stock_id = dcs.stock_id
 -- and bbd.date_price = dcs.date_price?

删除 values 子句:

INSERT INTO decisions (stock_id,date_price,created_date, last_updated_date, d20_l_d50)
select date_price, stock_id, now(), now(), adj_close_price > ub1_50_d
FROM bollinger_bands;