使用SQL插入大数据值

时间:2015-03-27 21:39:23

标签: sql sqlite interpolation

我对SQL和大数据都很新,所以请耐心等待。我有一张价值一年的天气数据表。我正在研究一天中每小时的温度和降水量(因此大约9000个值),但问题是气象站每6小时只记录降水量。所以,我想插入时间点并获得值,但我不知道如何去做。任何帮助将不胜感激!这是表格的一小部分:

Year Month Day Hour Temperature Precipitation 2014 1 1 0 -16.5444 0 2014 1 1 1 -10.3455 ***** 2014 1 1 2 -5.34855 ***** 2014 1 1 3 -15.4569 ***** 2014 1 1 4 -4.45666 ***** 2014 1 1 5 -11.2344 ***** 2014 1 1 6 -13.3454 0 2014 1 1 7 -20.2334 ***** 2014 1 1 8 -2.48555 ***** 2014 1 1 9 -5.43554 ***** 2014 1 1 10 -1.34555 ***** 2014 1 1 11 -9.00333 ***** 2014 1 1 12 -6.43555 0.04 2014 1 1 13 -12.3423 ***** 2014 1 1 14 -8.56664 ***** 2014 1 1 15 -15.3498 ***** 2014 1 1 16 -19.2384 ***** 2014 1 1 17 -21.2348 ***** 2014 1 1 18 -23.8778 0.31 2014 1 1 19 -20.8374 ***** 2014 1 1 20 -25.3874 ***** 2014 1 1 21 -21.3445 ***** 2014 1 1 22 -27.4599 ***** 2014 1 1 23 -30.9384 ***** 2014 1 2 0 -25.9085 0.15

2 个答案:

答案 0 :(得分:0)

我没有看到这样做的快捷方式,但我确实看到了一种方法。

以下是您需要的东西:

2个新表,2个填充表的程序和1个在实际表中插值的程序。

遵循以下3个步骤:

1:在第一个程序中,您想要通过天气表并收集所有沉淀物(仅每6小时一次,而不是*****),并将它们插入第一张表中

2:在第二个过程中,您要填充插值的第二个表。在这里你需要做一些数学计算才能确定要插入的数字

3:在最后一个程序中,您想要浏览Weather表并将每个*****值替换为第二个表中的相应数字(在步骤2中填写)。

为此,您需要在程序中使用CURSOR。插值完成后,您可以删除在步骤1和2中创建的2个表。

我希望这很清楚。可能有一种更简单的方法,但不是我所知道的。如果有什么不清楚请求评论的精确度,我会编辑我的答案。

答案 1 :(得分:0)

如果您想要的输出是:

Year        Month       Day         hour        Precipitation_interpolated
----------  ----------  ----------  ----------  --------------------------
2014        1           1           0           0.0
2014        1           1           1           0.0
2014        1           1           2           0.0
2014        1           1           3           0.0
2014        1           1           4           0.0
2014        1           1           5           0.0
2014        1           1           6           0.0
2014        1           1           7           0.00666666666666667
2014        1           1           8           0.0133333333333333
2014        1           1           9           0.02
2014        1           1           10          0.0266666666666667
2014        1           1           11          0.0333333333333333
2014        1           1           12          0.04
2014        1           1           13          0.085
2014        1           1           14          0.13
2014        1           1           15          0.175
2014        1           1           16          0.22
2014        1           1           17          0.265
2014        1           1           18          0.31
2014        1           1           19          0.283333333333333
2014        1           1           20          0.256666666666667
2014        1           1           21          0.23
2014        1           1           22          0.203333333333333
2014        1           1           23          0.176666666666667
2014        1           2           0           0.15

然后下面的查询就会产生。由于依赖于行编号/分区的大量子查询(SQLite没有本机支持),它可能效率不高,并且它使用在3.8.3版本中添加的公共表表达式(2014年发布) -02-03;如果您使用的是旧版本,则需要升级或重写查询以不使用常用的表格表达式 - 应该可以,但我没有尝试过。“

查询使用表t作为来源;它只在第一个select语句的from子句中引用,并且我假设样本数据中的*****null值的占位符:

with src as (
    select year, month, day, hour, precipitation, strftime('%Y-%m-%d %H:%M:%S', year ||'-'|| case when length(month) = 1 then 0||month else month end || '-' ||case when length(day) = 1 then 0||day else day end || case when length(hour) = 1 then 0||hour else hour end ||':00:00') as dt from t
), sample_data as (
        select year, month, day, hour, dt, Precipitation, (select count(*) from src b where a.dt >= dt) as id from src a 
), value_rows as (
    select id, Precipitation,
    (select count(*) from src b where val.dt >= dt and Precipitation is not null ) rn
    from (
        select id, Precipitation, dt
        from sample_data
        where Precipitation is not null
    ) val
), step_change as (
    select c1.id id_Start
        , c2.id - 1 id_End
        , c1.Precipitation
        , (c2.Precipitation - c1.Precipitation)/(c2.id - c1.id) change
    From value_rows c1
    inner join value_rows c2
    on c1.rn = c2.rn - 1
), interpolated_values as (
    select s.id
        , s.Year
        , s.Month
        , s.Day
        , s.Hour
        , s.Precipitation as value1
        , coalesce(sc.Precipitation, s.Precipitation) Precipitation
        , coalesce(sc.change, 0) change
        , coalesce((select count(*) from src b where s.dt >= dt) - id_Start, 0) coeff
        , sc.id_Start ,sc.id_End
    from sample_data s
    left outer join step_change sc
    on s.id between sc.id_Start and sc.id_End
) 

select Year, Month, Day, hour, Precipitation + coeff * change Precipitation_interpolated
from interpolated_values
order by id;

我在SQL Server MSDN论坛(here)上找到的t-sql查询中修补了SQLite版本。