【1】问题场景原代码

引起问题的原代码,访问数据库,汇总数据后,使用汇总结果报异常:

     local function amount_sum_fee(cycleid)
         local select_productid = "select product_id from np_cdr_" .. cycleid .. " group by product_id;"
         local rtn, productid_set = executeSql(select_productid)
         if rtn then

             local sql_const = "insert into compare_fee_" .. cycleid .. " (product_id, np_id, np_sum_duration, np_total_times, " ..
             " np_sum_qty, np_sum_fee, un_sum_duration, un_total_times, un_sum_fee, sum_np_fee, sum_original_fee) values "
             if type(productid_set) == 'table' and next(productid_set) ~= nil then
                 for key, record in pairs(productid_set) do
                     local amount_table = {}
                     amount_table["product_id"] = record["product_id"]
                     amount_table["np_id"] = 'all'
                     -- np总费用统计
                     local np_sum_fee = "SELECT product_id, SUM(duration) AS np_sum_duration, " ..
                     " COUNT(id) AS np_sum_cdrCnt, " ..
                     " SUM(service_qty) AS np_service_qty, " ..
                     " SUM(service_fee) AS np_service_fee, " ..
                     " SUM(np_fee) AS np_sum_fee, " ..
                     " SUM(original_fee) AS np_sum_original_fee " ..
                     " FROM np_cdr_" .. cycleid .. " WHERE product_id = '" .. record["product_id"] .. "';"
                     local rtn, desc = executeSql(np_sum_fee)
                     if rtn and next(desc) ~= nil then
                         amount_table[]["np_sum_duration"]
                         amount_table[]["np_sum_cdrCnt"]
                         amount_table[]["np_service_qty"]
                         amount_table[]["np_service_fee"]
                         amount_table[]["np_sum_fee"]
                         amount_table[]["np_sum_original_fee"]
                     end
                     -- un总费用统计
                     local un_sum_fee = "SELECT product_id, SUM(total_duration) AS un_sum_duration, " ..
                     " SUM(total_times) AS un_sum_cdrCnt, " ..
                     " SUM(total_fee) AS un_sum_fee " ..
                     " FROM dat_bill_" .. cycleid .. " WHERE call_type & 4097 > 0 AND product_id = '" .. record["product_id"] .. "';"
                     local rtn, desc = executeSql(un_sum_fee)
                     if rtn and next(desc) ~= nil then
                         amount_table[]["un_sum_duration"]
                         amount_table[]["un_sum_cdrCnt"]
                         amount_table[]["un_sum_fee"]
                     end
                     -- 构建sql语句
                      then
                         insert_sql = insert_sql .. ","
                     else
                         insert_sql = sql_const
                     end
                     insert_sql = insert_sql .. "('" ..
                     amount_table["product_id"] .. "', '" ..
                     amount_table["np_id"] .. "', " ..
                     amount_table["np_sum_duration"] .. ", " ..
                     amount_table["np_total_times"] .. ", " ..
                     amount_table["np_sum_qty"] .. ", " ..
                     amount_table["np_sum_fee"] .. ", " ..
                     amount_table["un_sum_duration"] .. ", " ..
                     amount_table["un_total_times"].. ", " ..
                     amount_table["un_sum_fee"] .. ", " ..
                     amount_table["sum_np_fee"] .. ", " ..
                     amount_table["sum_original_fee"] .. ")"
                     value_count = value_count +
                      == value_count then
                         sum_count = sum_count + value_count
                         insert_sql = insert_sql .. "; commit;"
                         local rtn, result = executeSql(insert_sql)
                         if not rtn then
                             rtnstr[
                             rtnstr["msg"] = "Error. amount_sum_fee insert into sum_fee num : " .. sum_count .. " sql : " .. insert_sql
                             return false
                         else
                             local result = "compare: success to amount_sum_fee insert into sum_fee num : " .. sum_count
                             ngx.log(ngx.ERR, result)
                         end
                         insert_sql = ""
                         value_count =
                     end
                 end
                  then
                     sum_count = sum_count + value_count
                     insert_sql = insert_sql .. "; commit;"
                     local rtn, result = executeSql(insert_sql)
                     if not rtn then
                         rtnstr[
                         rtnstr["msg"] = "Error. amount_sum_fee insert into sum_fee num : " .. sum_count .. " sql : " .. insert_sql
                         return false
                     else
                         local result = "compare: success to amount_sum_fee insert into sum_fee num : " .. sum_count
                         ngx.log(ngx.ERR, result)
                     end
                     insert_sql = ""
                     value_count =
                 end
             end
         else
             rtnstr[
             rtnstr["msg"] = "Failed to select product_id amount_sum_fee error : " .. productid_set
             return false
         end
         return true
     end

使用的是SQL中的sum函数

【2】分析原因

数据库访问汇总数据时,SUM函数的结果可能为NULL值。如下图:

修改SQL语句,添加IFNULL判断,若为NULL,则为0即可。如下图:

如上原因分析。

【3】修复后代码

     local function amount_sum_fee(cycleid)
         local select_productid = "select product_id from np_cdr_" .. cycleid .. " group by product_id;"
         local rtn, productid_set = executeSql(select_productid)
         if rtn then

             local sql_const = "insert into compare_fee_" .. cycleid .. " (product_id, np_id, np_sum_duration, np_total_times, " ..
             " np_sum_qty, np_sum_fee, un_sum_duration, un_total_times, un_sum_fee, sum_np_fee, sum_original_fee) values "
             if type(productid_set) == 'table' and next(productid_set) ~= nil then
                 for key, record in pairs(productid_set) do
                     local amount_table = {}
                     amount_table["product_id"] = record["product_id"]
                     amount_table["np_id"] = 'all'
                     -- np总费用统计
                     local np_sum_fee = "SELECT product_id, IFNULL(SUM(duration), 0) AS np_sum_duration, " ..
                     " COUNT(id) AS np_sum_cdrCnt, " ..
                     " IFNULL(SUM(service_qty), 0) AS np_service_qty, " ..
                     " IFNULL(SUM(service_fee), 0) AS np_service_fee, " ..
                     " IFNULL(SUM(np_fee), 0) AS np_sum_fee, " ..
                     " IFNULL(SUM(original_fee), 0) AS np_sum_original_fee " ..
                     " FROM np_cdr_" .. cycleid .. " WHERE product_id = '" .. record["product_id"] .. "';"
                     local rtn, desc = executeSql(np_sum_fee)
                     if rtn and next(desc) ~= nil then
                         amount_table[]["np_sum_duration"]
                         amount_table[]["np_sum_cdrCnt"]
                         amount_table[]["np_service_qty"]
                         amount_table[]["np_service_fee"]
                         amount_table[]["np_sum_fee"]
                         amount_table[]["np_sum_original_fee"]
                     end
                     -- un总费用统计
                     local un_sum_fee = "SELECT product_id, IFNULL(SUM(total_duration), 0) AS un_sum_duration, " ..
                     " IFNULL(SUM(total_times), 0) AS un_sum_cdrCnt, " ..
                     " IFNULL(SUM(total_fee), 0) AS un_sum_fee " ..
                     " FROM dat_bill_" .. cycleid .. " WHERE call_type & 4097 > 0 AND product_id = '" .. record["product_id"] .. "';"
                     local rtn, desc = executeSql(un_sum_fee)
                     if rtn and next(desc) ~= nil then
                         amount_table[]["un_sum_duration"]
                         amount_table[]["un_sum_cdrCnt"]
                         amount_table[]["un_sum_fee"]
                     end
                     -- 构建sql语句
                      then
                         insert_sql = insert_sql .. ","
                     else
                         insert_sql = sql_const
                     end
                     insert_sql = insert_sql .. "('" ..
                     amount_table["product_id"] .. "', '" ..
                     amount_table["np_id"] .. "', " ..
                     amount_table["np_sum_duration"] .. ", " ..
                     amount_table["np_total_times"] .. ", " ..
                     amount_table["np_sum_qty"] .. ", " ..
                     amount_table["np_sum_fee"] .. ", " ..
                     amount_table["un_sum_duration"] .. ", " ..
                     amount_table["un_total_times"].. ", " ..
                     amount_table["un_sum_fee"] .. ", " ..
                     amount_table["sum_np_fee"] .. ", " ..
                     amount_table["sum_original_fee"] .. ")"
                     value_count = value_count +
                      == value_count then
                         sum_count = sum_count + value_count
                         insert_sql = insert_sql .. "; commit;"
                         local rtn, result = executeSql(insert_sql)
                         if not rtn then
                             rtnstr[
                             rtnstr["msg"] = "Error. amount_sum_fee insert into sum_fee num : " .. sum_count .. " sql : " .. insert_sql
                             return false
                         else
                             local result = "compare: success to amount_sum_fee insert into sum_fee num : " .. sum_count
                             ngx.log(ngx.ERR, result)
                         end
                         insert_sql = ""
                         value_count =
                     end
                 end
                  then
                     sum_count = sum_count + value_count
                     insert_sql = insert_sql .. "; commit;"
                     local rtn, result = executeSql(insert_sql)
                     if not rtn then
                         rtnstr[
                         rtnstr["msg"] = "Error. amount_sum_fee insert into sum_fee num : " .. sum_count .. " sql : " .. insert_sql
                         return false
                     else
                         local result = "compare: success to amount_sum_fee insert into sum_fee num : " .. sum_count
                         ngx.log(ngx.ERR, result)
                     end
                     insert_sql = ""
                     value_count =
                 end
             end
         else
             rtnstr[
             rtnstr["msg"] = "Failed to select product_id amount_sum_fee error : " .. productid_set
             return false
         end
         return true
     end

【4】总结

Lua中使用SQL语句进行汇总数据时,切记需要作NULL判断。

Good Good Study, Day Day Up.

顺序 选择 循环 总结

lua entry thread aborted: runtime error: /usr/../process.lua:448: attempt to concatenate field 'np_sum_duration' (a userdata value)的更多相关文章

  1. openresty 报错:lua entry thread aborted: runtime error

    [1]问题现象 (1)本地openresty系统 (2)报错信息 2019/09/10 08:13:55 [error] 2385#2385: *4 lua entry thread aborted: ...

  2. /usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "process_linux.go:245: running exec setns .....

    docker创建容器时报错如下: containerd: start container" error="oci runtime error: container_linux.go ...

  3. centos7.2部署docker-17.06.0-ce的bug:Error response from daemon: oci runtime error: container_linux.go:262: starting container process caused "process_linux.go:339: container init caused \"\"".

    现象: 操作系统:centos 7.2 kernel 3.10.0-327.el7.x86_64 mesos:1.3.0 docker:docker-17.06.0-ce 在做mesos验证时,通过m ...

  4. error: /usr/include/objc/objc-class.h: No such file or directory

    When i use the example of ShareKit package,i have come across this error:"error: /usr/include/o ...

  5. Heka 编译安装后 运行报错 panic: runtime error: cgo argument has Go pointer to Go pointer

    Heka 编译安装后 运行报错 panic: runtime error: cgo argument has Go pointer to Go pointer 解决办法: 1.  Start heka ...

  6. ERROR: child process failed, exited with error number 100

    [root@localhost ~]# mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --loga ...

  7. mongodb启动时报错ERROR: child process failed, exited with error number 1

    不多说,直接上干货! 前期博客 Ubuntu14.04下Mongodb安装部署步骤(图文详解) Ubuntu16.04下Mongodb安装部署步骤(图文详解) root@zhouls-virtual- ...

  8. Finished with error: ProcessException: Process "D:\FlutterAPP\flutter_appfive\android\gradlew.bat" exited abnormally:

    在使用Flutter进行开发是遇到这样一个问题 Finished with error: ProcessException: Process "D:\FlutterAPP\flutter_a ...

  9. lua.c:82:10: fatal error: readline/readline.h: No such file or directory #include <readline/readline.h>

    make linuxcd src && make linuxmake[1]: Entering directory `/root/lua/lua-5.3.2/src'make all ...

随机推荐

  1. PHP之面向对象(下)

    1,类的创建 class 2,对象的创建 new关键字 3,成员的添加 修饰符 添加成员需要三个修饰符 public 公开的 定义公共的属性和方法,类的外部,内部,子类都可以使用 protected ...

  2. 微信小程序环境配置和开发!!

    1.登陆微信公众平台小程序,下载 普通小程序开发者工具.或者 小游戏开发者工具. 2.新建项目需要填以下几点,然后初始demo如下,注意rpx是分成750份的单位. 3.点击预览,用微信扫描二维码,代 ...

  3. VMware + CentOS 7搭建环境(二)

    1.环境要求建议使用VMwareWorkstation虚拟机软件:可以使用快照功能,保存虚拟机状态:本文档示例版本10.0.1:1.2 CentOS系统的iso文件; 下载好的.iso的压缩文件格式, ...

  4. MES助力伊利集团打造智慧工厂

    1.项目背景介绍 在国家政策和事业部.工厂的实际需求双重背景下,2016年7-9月期间,伊利集团信息部门.业务部门,先后与国内外领先的设备和咨询公司进行了智能制造.智慧工厂等话题的沟通交流,并组织实地 ...

  5. 3.redis 都有哪些数据类型?分别在哪些场景下使用比较合适?

    作者:中华石杉 面试题 redis 都有哪些数据类型?分别在哪些场景下使用比较合适? 面试官心理分析 除非是面试官感觉看你简历,是工作 3 年以内的比较初级的同学,可能对技术没有很深入的研究,面试官才 ...

  6. HDU 1548 A strange lift 题解

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. JfreeChart 乱码问题处理

    在前面之间加上下面这段代码即可. //创建主题样式 StandardChartTheme standardChartTheme=new StandardChartTheme("CN" ...

  8. 爬虫篇-python爬虫中多线程的使用

    queue介绍 queue是python的标准库,俗称队列.可以直接import引用,在python2.x中,模块名为Queue.python3直接queue即可 在python中,多个线程之间的数据 ...

  9. tensorflow运行时错误:服务似乎挂掉了,但是会立刻重启的.

    以前在POD里跑起来,没问题的示例代码. 移到jupyter中,多给两个GPU,有时运行就会爆出这个错误: 于是,按网上的意见,暂时加了个使用GPU的指定, 暂时搞定. 如下红色部分. import ...

  10. 第03节-BLE协议各层数据格式概述

    本篇博客根据韦大仙的视频,整理所得. 对于BLE系统,它分为上下两块.上面那一块,我们称为host主机.下面这一块是controller,你可以简单的认为它就是一个蓝牙芯片. 对于host这一块,它运 ...