#工作需要,很不流利的写了几个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

 

posted on 2020-08-08 17:56  追他十万八千里  阅读(432)  评论(0编辑  收藏  举报