SUM()在select语句中工作正常,但在Insert语句中返回NULL

时间:2013-06-16 15:24:59

标签: sql insert null sum

我正在创建一个脚本,通过某些ID Matrix ID来总结利润和损失(PL)。我对视图“vw_package”使用以下SQL语句:

select trade_matrix_id, sum(pl) from vw_package 
where date (gen_timestamp) between '2012-05-01' and '2012-05-30' 
and trade_matrix_id between 30 and 60
group by trade_matrix_id;

它正确地产生了这个结果:

trade_matrix_id,sum(pl)
41,            -147.33
42,             -27.45
43,            -329.03
44,            -329.03
45,            -329.03
...

现在我想填写一张完全符合这些结果的表格。我在我的脚本中创建了一个表:

CREATE TABLE IF NOT EXISTS PLByMatrixID 
    (trade_matrix_id    int,
    Sum_PL              float(19,2));

我使用INSERT命令用与上面完全相同的SELECT语句填充表:

insert into PLByMatrixID (trade_matrix_id, Sum_PL)
   select trade_matrix_id, sum(pl) from vw_package 
   where date (gen_timestamp) between '2012-05-01' and '2012-05-30' 
   and trade_matrix_id between 30 and 60
   group by trade_matrix_id;

我调用脚本并查询结果tablePLByMatrixID,PL中的所有值都为NULL:

41,            NULL
42,            NULL
43,            NULL
...

如果有人能告诉我这里缺少什么,我感激不尽。

1 个答案:

答案 0 :(得分:0)

尝试

   select trade_matrix_id, sum(pl)
   into PLByMatrixID 
   from vw_package 
   where date (gen_timestamp) between '2012-05-01' and '2012-05-30' 
   and trade_matrix_id between 30 and 60
   group by trade_matrix_id;