【MySql】存储过程限定月份,限定某天等基础的使用
2015-08-31 09:47
901 查看
[sql] view
plaincopyprint?
BEGIN
declare begin_time varchar(10); /****一月的第一天**/
declare end_time varchar(10); /****月的最后一天**/
declare month_time varchar(10); /****月份,格式:2013-11**/
declare releaseCount int default 0; /***首页开屏广告点击量*/
declare math int default 0;
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');
prepare stmt from @execSql;
execute stmt;
#set @end_time = dataTime;
set @end_time = concat(substring(dataTime,1,7),'-31');
set @begin_time = concat(substring(@end_time,1,7),'-01');
set @month_time = substring(@end_time,1,7);
/**********数字的使用********/
set @math =\'2102\' ;
/**********字符 或 变量 的使用********/
set @math =\'',dataTime,'\' ;
/************限定insert_time日的*******************/
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\' ');
/************限定data_time月份的*******************/
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');
/****设置值的写法1*****/
set @execSql = concat('insert into REP_CLICK_COUNT(data_time,release_count,forum_count,h5_count,add_count,admin_forum_count)',
' values(\'',@month_time,'\',\'',@releaseCount,'\',\'',@forumCount,'\',\'',@h5Count,'\',\'',@addCount,'\',\'',@adminforumCount,'\',now())');
prepare stmt from @execSql;
execute stmt;
/****设置值的写法2*****/
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) ',
' 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 ');
prepare stmt from @execSql;
execute stmt;
/************练习1*******************/
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 ',
' 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\' ');
prepare stmt from @execSql;
execute stmt;
/************练习2*******************/
set @execSql = concat('insert into fact_active_month(data_time,channel_code,version,active_count,insert_time) ',
' select \'',@month_time,'\', channel_code,\'all\',count(DISTINCT device_imei,device_mac),now() ',
' 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');
prepare stmt from @execSql;
execute stmt;
END
plaincopyprint?
BEGIN
declare begin_time varchar(10); /****一月的第一天**/
declare end_time varchar(10); /****月的最后一天**/
declare month_time varchar(10); /****月份,格式:2013-11**/
declare releaseCount int default 0; /***首页开屏广告点击量*/
declare math int default 0;
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');
prepare stmt from @execSql;
execute stmt;
#set @end_time = dataTime;
set @end_time = concat(substring(dataTime,1,7),'-31');
set @begin_time = concat(substring(@end_time,1,7),'-01');
set @month_time = substring(@end_time,1,7);
/**********数字的使用********/
set @math =\'2102\' ;
/**********字符 或 变量 的使用********/
set @math =\'',dataTime,'\' ;
/************限定insert_time日的*******************/
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\' ');
/************限定data_time月份的*******************/
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');
/****设置值的写法1*****/
set @execSql = concat('insert into REP_CLICK_COUNT(data_time,release_count,forum_count,h5_count,add_count,admin_forum_count)',
' values(\'',@month_time,'\',\'',@releaseCount,'\',\'',@forumCount,'\',\'',@h5Count,'\',\'',@addCount,'\',\'',@adminforumCount,'\',now())');
prepare stmt from @execSql;
execute stmt;
/****设置值的写法2*****/
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) ',
' 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 ');
prepare stmt from @execSql;
execute stmt;
/************练习1*******************/
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 ',
' 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\' ');
prepare stmt from @execSql;
execute stmt;
/************练习2*******************/
set @execSql = concat('insert into fact_active_month(data_time,channel_code,version,active_count,insert_time) ',
' select \'',@month_time,'\', channel_code,\'all\',count(DISTINCT device_imei,device_mac),now() ',
' 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');
prepare stmt from @execSql;
execute stmt;
END
相关文章推荐
- MySQL基本操作(三):全文本搜索
- MySQL主从配置
- 【MySql】 MySQL存储过程常用的函数(字符串类型操作,数学类,日期时间类)
- 【MySql】MySQL的转义字符“\” \'使用说明
- 【MySql】MySql基础使用详解
- 【mysql】mysql 小数转换成百分数查出(保留两位小数百分数)以及怎么使select中嵌套if的使用
- 【mysql】Mysql数据库备份和按条件导出表数据
- MySQL索引 查看、创建、删除
- mysql如何修改所有的definer
- MySQL和MSSQL差异(增量)备份的原理
- MySQL常用操作命令备忘
- 用命令行导出和导入MySQL数据库
- MySQL数据库备份以及常用备份工具集合
- windows下如何安装和启动MySQL
- sql语句修改mysql表的自增主键
- Mysql 大小写问题
- mysql编译安装
- mysql authorization check
- mysql backup script
- MySQL加载JDBC驱动程序