zoukankan      html  css  js  c++  java
  • 计算通话时长

    #工作需要,很不流利的写了几个sql,最后完成了基本的功能,辛苦编辑,做个备份。

    select * from top_sales;
    select SUBSTRING_INDEX(call_duration,':',1) from top_sales; #第一列时间
    select SUBSTRING_INDEX(SUBSTRING_INDEX(call_duration,':',-2),':',1) from top_sales; #第二列时间
    select SUBSTRING_INDEX(call_duration,':',-1) from top_sales; #第三列时间

    #加0转数字
    #select SUBSTRING_INDEX(call_duration,':',1)+0 a from top_sales; #第一列时间
    #select SUBSTRING_INDEX(SUBSTRING_INDEX(call_duration,':',-2),':',1)+0 b from top_sales; #第二列时间
    #select SUBSTRING_INDEX(call_duration,':',-1)+0 c from top_sales; #第三列时间

    #每一列时间求和
    #select sum(a) from (select SUBSTRING_INDEX(call_duration,':',1)+0 a from top_sales)d; #第一列时间
    #select sum(b) from (select SUBSTRING_INDEX(SUBSTRING_INDEX(call_duration,':',-2),':',1)+0 b from top_sales)e; #第二列时间
    #select sum(c) from (select SUBSTRING_INDEX(call_duration,':',-1)+0 c from top_sales)f; #第三列时间

    #每一列时间转化成秒
    #select sum(a)*3600 from (select SUBSTRING_INDEX(call_duration,':',1)+0 a from top_sales)d; #第一列时间
    #select sum(b)*60 from (select SUBSTRING_INDEX(SUBSTRING_INDEX(call_duration,':',-2),':',1)+0 b from top_sales)e; #第二列时间
    #select sum(c) from (select SUBSTRING_INDEX(call_duration,':',-1)+0 c from top_sales)f; #第三列时间

    #添加一个id然后连接起来
    #select id,sum(a)*3600 j from (select id,SUBSTRING_INDEX(call_duration,':',1)+0 a from top_sales)d; #第一列时间
    #select id,sum(b)*60 h from (select id,SUBSTRING_INDEX(SUBSTRING_INDEX(call_duration,':',-2),':',1)+0 b from top_sales)e; #第二列时间
    #select id,sum(c) i from (select id,SUBSTRING_INDEX(call_duration,':',-1)+0 c from top_sales)f; #第三列时间

    #连接起来(表一定要起一个别名)

    select * from (
    (select id,sum(a)*3600 j from (select id,SUBSTRING_INDEX(call_duration,':',1)+0 a from top_sales)d)j,
    (select id,sum(b)*60 h from (select id,SUBSTRING_INDEX(SUBSTRING_INDEX(call_duration,':',-2),':',1)+0 b from top_sales)e)k,
    (select id,sum(c) i from (select id,SUBSTRING_INDEX(call_duration,':',-1)+0 c from top_sales)f)l
    )
    where j.id=k.id=l.id;
    #计算总的秒数/3600
    select (j+h+i)/3600 from (
    (select id,sum(a)*3600 j from (select id,SUBSTRING_INDEX(call_duration,':',1)+0 a from top_sales)d)j,
    (select id,sum(b)*60 h from (select id,SUBSTRING_INDEX(SUBSTRING_INDEX(call_duration,':',-2),':',1)+0 b from top_sales)e)k,
    (select id,sum(c) i from (select id,SUBSTRING_INDEX(call_duration,':',-1)+0 c from top_sales)f)l
    )
    where j.id=k.id=l.id;

    #计算人均时长
    select (j+h+i)/3600/rs 平均时长 from (
    (select id,sum(a)*3600 j from (select id,SUBSTRING_INDEX(call_duration,':',1)+0 a from top_sales)d)j,
    (select id,sum(b)*60 h from (select id,SUBSTRING_INDEX(SUBSTRING_INDEX(call_duration,':',-2),':',1)+0 b from top_sales)e)k,
    (select id,sum(c) i from (select id,SUBSTRING_INDEX(call_duration,':',-1)+0 c from top_sales)f)l,
    (select id,count(*)rs from top_sales)m
    )
    where j.id=k.id=l.id=m.id;

    select left(pjsc,5)平均时长,left(n.pjsc,1)小时,concat('0.',SUBSTRING(n.pjsc,3,4))*60 分钟
    from(
    select (j+h+i)/3600/rs pjsc from (
    (select id,sum(a)*3600 j from (select id,SUBSTRING_INDEX(call_duration,':',1)+0 a from top_sales)d)j,
    (select id,sum(b)*60 h from (select id,SUBSTRING_INDEX(SUBSTRING_INDEX(call_duration,':',-2),':',1)+0 b from top_sales)e)k,
    (select id,sum(c) i from (select id,SUBSTRING_INDEX(call_duration,':',-1)+0 c from top_sales)f)l,
    (select id,count(*)rs from top_sales)m
    )
    where j.id=k.id=l.id=m.id
    )n

  • 相关阅读:
    ZABBIX自动发现添加主机功能
    Kafka史上最详细原理总结
    python的基本函数
    windows和linux出现timewait过多的解决方法
    Ext.Net使用DirectMethod.request调用一般处理程序(.ashx)并传递参数
    $.post、$.get、$.ajax三者的区别
    .net Web应用程序添加WebService引用时报错的问题
    ASP.NET TreeView控件各个节点总是居中对齐,而不是左对齐的问题
    利用Win8上的IIS来部署ASP网站
    做文件目录DEMO时发现的问题
  • 原文地址:https://www.cnblogs.com/timingstarts/p/13458747.html
Copyright © 2011-2022 走看看