【MySql】存储过程限定月份,限定某天等基础的使用

来源:互联网 发布:statistics软件 编辑:程序博客网 时间:2024/04/28 14:18
[sql] view plaincopyprint?
  1. BEGIN  
  2.     declare begin_time varchar(10);  /****一月的第一天**/  
  3.     declare end_time varchar(10);    /****月的最后一天**/  
  4.     declare month_time varchar(10);      /****月份,格式:2013-11**/  
  5.   
  6.     declare releaseCount int default 0; /***首页开屏广告点击量*/  
  7.     declare math int default 0;  
  8.   
  9.     set @execSql = concat(' select ifnull( count(*) , 0 ) into @releaseCount from REP_CLICK_COUNT where data_time>=\'',@begin_time,'\' and data_time<=\'',@end_time,'\' and DATA_TYPE=1');  
  10.     prepare stmt from @execSql;  
  11.     execute stmt;  
  12.       
  13.     #set @end_time = dataTime;  
  14.     set @end_time = concat(substring(dataTime,1,7),'-31');  
  15.   
  16.     set @begin_time = concat(substring(@end_time,1,7),'-01');  
  17.   
  18.     set @month_time = substring(@end_time,1,7);  
  19.   
  20.    /**********数字的使用********/  
  21.     set @math =\'2102\' ;  
  22.   
  23.     /**********字符 或 变量 的使用********/  
  24.      set @math =\'',dataTime,'\' ;  
  25.   
  26.    /************限定insert_time日的*******************/  
  27.     where str_to_date(concat(\'',dataTime,'\',\'00:00:00\'),\'%Y-%m-%d %H:%i:%s\')<=insert_time ',' and insert_time<=str_to_date(concat(\'',dataTime,'\',\'23:59:59\'),\'%Y-%m-%d %H:%i:%s\') and version=\'2\' and event_id=\'2102\'  and event_source=\'2006\'  ');   
  28.   
  29.    /************限定data_time月份的*******************/  
  30.     where data_time>=\'',@begin_time,'\' and data_time<=\'',@end_time,'\' and (action_type=1 or action_type=2)  and (client_name=\'aMarket2.0\' or client_name=\'aMarket30_Pad\' or client_name=\'aMarketSDK\' or client_name=\'aMarket_Pad\' or client_name=\'GfanSDK\')  GROUP BY channel_code  ORDER BY count(DISTINCT device_imei,device_mac) DESC');   
  31.   
  32.    /****设置值的写法1*****/  
  33.     set @execSql = concat('insert into REP_CLICK_COUNT(data_time,release_count,forum_count,h5_count,add_count,admin_forum_count)',  
  34.                                     ' values(\'',@month_time,'\',\'',@releaseCount,'\',\'',@forumCount,'\',\'',@h5Count,'\',\'',@addCount,'\',\'',@adminforumCount,'\',now())');  
  35.     prepare stmt from @execSql;  
  36.     execute stmt;  
  37.   
  38.     /****设置值的写法2*****/  
  39.     set @execSql = concat('insert into report_gfan_column(data_time,version, column_code,click_count,down_count,click_down_rate,install_count,down_install_rate,insert_time) ',  
  40.                        ' select data_time, client_version,data_type, count, 0,0.00,0,0.00,now() from report_gfan_to_column where data_time=\'',dataTime,'\' and data_type<=20 ');   
  41.   
  42.     prepare stmt from @execSql;  
  43.     execute stmt;  
  44.   
  45.    /************练习1*******************/  
  46.     set @execSql = concat('insert into report_gfan_data(data_time,client_version_code,client_version,data_type,count) select \'',dataTime,'\', \'0\', \'0\', 48, count(1) from client_event_log ',  
  47.                                         ' where str_to_date(concat(\'',dataTime,'\',\'00:00:00\'),\'%Y-%m-%d %H:%i:%s\')<=insert_time ',  
  48.                     ' and insert_time<=str_to_date(concat(\'',dataTime,'\',\'23:59:59\'),\'%Y-%m-%d %H:%i:%s\') and version=\'2\' and event_id=\'2102\'  and event_source=\'2006\'  ');   
  49.   
  50.     prepare stmt from @execSql;  
  51.     execute stmt;  
  52.           
  53.    /************练习2*******************/  
  54.    set @execSql = concat('insert into  fact_active_month(data_time,channel_code,version,active_count,insert_time) ',  
  55.                                 ' select  \'',@month_time,'\', channel_code,\'all\',count(DISTINCT device_imei,device_mac),now() ',  
  56.                                 ' from log_device_active where data_time>=\'',@begin_time,'\' and data_time<=\'',@end_time,'\' and (action_type=1 or action_type=2)  and (client_name=\'aMarket2.0\' or client_name=\'aMarket30_Pad\' or client_name=\'aMarketSDK\' or client_name=\'aMarket_Pad\' or client_name=\'GfanSDK\')  GROUP BY channel_code  ORDER BY count(DISTINCT device_imei,device_mac) DESC');   
  57.     prepare stmt from @execSql;  
  58.     execute stmt;  
  59. END  
0 0
原创粉丝点击