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

  • 相关阅读:
    09不可变类型
    08浅拷贝和深拷贝
    07参数的传递
    06判等对象是否相等
    05引用类型以及特殊引用类型string
    报错:Missing type map configuration or unsupported mapping
    黄聪:VS2010开发T4模版引擎之基础入门
    黄聪:《网站高转换率法则》#2:你的网站是垃圾站吗?
    黄聪:《网站高转换率法则》#1:为什么要研究网站转换率?
    黄聪:百度知道中对HTML字符实体、字符编号,&开头字符的使用
  • 原文地址:https://www.cnblogs.com/timingstarts/p/13458747.html
Copyright © 2011-2022 走看看