天气数据处理

1.site_code 取前五位

(因为是先用文件名作为site_code,然后从文件名中截取前五位即为site_code)

image

image

(图上错了,图上取得是6位,但是懒得在找一遍截图了)

2.存储过程+游标对数据进行处理

(1)缺失数据(-9999)置为一个大概值

(2)将10比例的数据除以10,例如温度220→22

image

(好家伙,处理了一下午)

image

3.删除列

游标更新的时候忘了sky和wind_direction了,这两列问题不大,直接删了

image

image

4.SQL语句

(1)截取

update site_weather set site_code=left(site_code,5)

(2)创建索引

create index index_id on site_weather(id desc)

(3)存储过程

drop procedure processData;
/*创建存储过程,使用游标处理site_weather中的数据*/
create procedure processData() begin
/*声明临时变量,用于更改*/
    declare result_code integer default 1; #定义返回结果并赋初值0
    declare s int default 0;               #标志变量 
    declare resultId bigint default 0;                                                         
    declare air int default 0;
    declare dew int default 0;
    declare sea int default 0;
    declare windDirection int default 0;
    declare windSpeed int default 0;
    declare sky int default 0;
    declare one int default 0;
    declare six    int default 0;    

/*声明游标*/
    declare processCursor cursor for select 
    id,air_temperature,dew_point_temperature,sea_level_pressure,wind_direction,wind_speed,sky_coverage,one_hour_duration,six_hour_duration
    from site_weather;

/*定义HANDLER*/
    declare continue handler for sqlexception set result_code=0; #在执行过程中出任何异常设置result_code为0
    declare continue handler for not found set s=1;              #声明当游标遍历完后将标志变量置成某个值

/*打开游标*/
    open processCursor;        

/*将游标中的值赋值给变量,注意:变量名不要和返回的列名同名,变量顺序要和sql结果列的顺序一致*/
fetch processCursor into  resultId,air,dew,sea,windDirection,windSpeed,sky,one,six;
    -- 当s不等于1,也就是未遍历完时,会一直循环
    while s<>1 do
        -- 执行业务逻辑
          /*
             *处理air_temperature为缺失数据时(-9999)
             *这里由于之前更新操作失误,因此-1000的值也是无效的,需要重新更新
          */
           if air=-9999 or  air=-1000 then
                 set air=200;
                 end if;
            /*
             *处理dew_point_temperature为缺失数据时(-9999)
            */
            if dew=-9999 or dew=-1000 then
                 set dew=200;
                 end if;
            /*
             *处理sea_level_pressure为缺失数据时(-9999)
            */
            if sea=-9999 or sea=-1000 then
                 set sea=10000;
                 end if;
            /*
             *处理wind_direction为缺失数据时(-9999)
            */
            if windDirection=-9999 or windDirection=-1000 then
                 set windDirection=180;
                 end if;
            /*
             *处理wind_speed为缺失数据时(-9999)
            */
            if windSpeed=-9999 or windSpeed=-1000 then
                 set windSpeed=40;
                 end if;
            /*
             *处理sky_coverage为缺失数据时(-9999)
            */
            if sky=-9999 or sky=-1000 then
                 set sky=5;
                 end if;
            /*
             *处理one_hour_duration为缺失数据时(-9999)
            */
            if one=-9999 or one=-1000 then
                 set one=0;
                 end if;
            /*
             *处理six_hour_duration为缺失数据时(-9999)
            */
            if six=-9999 or six=-1000 then
                 set six=0;
                 end if;
            
            /*
             *转换为现实数据(前面都只是在判断以及赋值,这里才是真正的更新语句)
            */
            update site_weather
            set air_temperature=air/10,dew_point_temperature=dew/10,
            sea_level_pressure=sea/10,wind_speed=windSpeed/10,
                one_hour_duration=one/10,six_hour_duration=six/10
            where id=resultId;
            
        -- 将游标中的值再赋值给变量,供下次循环使用
        fetch processCursor
        into  resultId,air,dew,sea,windDirection,windSpeed,sky,one,six;
        -- 当s等于1时表明遍历以完成,退出循环
    end while;
    
/*--关闭游标*/
close processCursor;

end

call processData();

(4)删除列

/*sky_coverage和wind_direction忘了更新了,就直接删掉了*/
alter table site_weather drop column sky_coverage;
alter table site_weather drop column wind_direction;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值