我正在努力将数据插入到辅助表中,它需要一个子查询来计算并将滚动关闭值插入到新表中。当我尝试查询时,我收到了两个错误之一:
我已经验证目标表具有insert语句中引用的所有列。任何帮助,将不胜感激。
INSERT INTO `Historical`.`Secondary`(
`Record`,
`Symbol`,
`Date`,
`Open`,
`High`,
`Low`,
`Close`,
`Volume`,
`Rolling_Close`)
SELECT
T1.Date, T1.Symbol, T1.Close,
(
SELECT AVG(T2.Close)
FROM Historical.Primary T2
WHERE T1.Symbol = T2.Symbol
AND T2.Date BETWEEN DATE_SUB(T1.Date, INTERVAL 30 DAY)
AND DATE_SUB(T1.Date, INTERVAL 1 DAY)) AS Rolling_Close
FROM Historical.Primary T1
GROUP BY T1.Date,
T1.Symbol;
表Historical.Secondary具有以下列:
记录,符号,日期,打开,高,低,关闭,音量,Rolling_Close
表Historical.Primary包含以下列:
记录,符号,日期,打开,高,低,关闭,量
答案 0 :(得分:0)
假设您的表和数据与此
类似drop table if exists secondary;
create table secondary (record int auto_increment primary key,symbol int,dt date,open int, high int, low int,close int,volume int, rolling_close decimal(10,2));
drop table if exists prmary;
create table prmary (record int auto_increment primary key,symbol int,dt date,open int, high int, low int,close int,volume int);
alter table prmary
add key k001(symbol,dt);
MariaDB [sandbox]> select * from prmary order by symbol,record;
+--------+--------+------------+------+------+------+-------+--------+
| record | symbol | dt | open | high | low | close | volume |
+--------+--------+------------+------+------+------+-------+--------+
| 1 | 707 | 2007-08-01 | NULL | NULL | NULL | 242 | NULL |
| 3 | 707 | 2007-08-02 | NULL | NULL | NULL | 7 | NULL |
| 5 | 707 | 2007-08-03 | NULL | NULL | NULL | 4 | NULL |
| 7 | 707 | 2007-08-04 | NULL | NULL | NULL | 2 | NULL |
| 2 | 712 | 2007-08-01 | NULL | NULL | NULL | 415 | NULL |
| 4 | 712 | 2007-08-02 | NULL | NULL | NULL | 4 | NULL |
| 6 | 712 | 2007-08-03 | NULL | NULL | NULL | 7 | NULL |
| 8 | 712 | 2007-08-04 | NULL | NULL | NULL | 5 | NULL |
+--------+--------+------------+------+------+------+-------+--------+
8 rows in set (0.00 sec)
此查询
truncate table secondary;
insert into secondary
(`Record`, `Symbol`, `Dt`,`Open`, `High`, `Low`,`Close`,`Volume`,`Rolling_Close`
)
SELECT record,T1.Symbol, T1.Dt,open, high, low,t1.close,volume,
(
SELECT avg(t2.close)
FROM Prmary T2
WHERE T1.Symbol = T2.Symbol
AND T2.Dt BETWEEN DATE_SUB(T1.Dt, INTERVAL 30 day)
AND DATE_SUB(T1.Dt, INTERVAL 0 day)
) AS Rolling_Close
FROM Prmary T1
GROUP BY t1.symbol,t1.dt;
产生此结果
MariaDB [sandbox]> select * from secondary order by symbol, record;
+--------+--------+------------+------+------+------+-------+--------+---------------+
| record | symbol | dt | open | high | low | close | volume | rolling_close |
+--------+--------+------------+------+------+------+-------+--------+---------------+
| 1 | 707 | 2007-08-01 | NULL | NULL | NULL | 242 | NULL | 242.00 |
| 3 | 707 | 2007-08-02 | NULL | NULL | NULL | 7 | NULL | 124.50 |
| 5 | 707 | 2007-08-03 | NULL | NULL | NULL | 4 | NULL | 84.33 |
| 7 | 707 | 2007-08-04 | NULL | NULL | NULL | 2 | NULL | 63.75 |
| 2 | 712 | 2007-08-01 | NULL | NULL | NULL | 415 | NULL | 415.00 |
| 4 | 712 | 2007-08-02 | NULL | NULL | NULL | 4 | NULL | 209.50 |
| 6 | 712 | 2007-08-03 | NULL | NULL | NULL | 7 | NULL | 142.00 |
| 8 | 712 | 2007-08-04 | NULL | NULL | NULL | 5 | NULL | 107.75 |
+--------+--------+------------+------+------+------+-------+--------+---------------+
8 rows in set (0.00 sec)