SQL语句练习

创建一张表,记录电话呼叫员的工作流水,记录呼叫员编号,对方号码,通话开始时间,
结束时间。建表,插数据等都自己写出sql代码

要求:
输出所有数据中通话时间最长的5条记录。
输出所有数据中拨打长途号码(对方号码以0开头)的总时长
输出本月通话时长最多的前三个呼叫员的编号
输出本月拨打电话次数最多的前三个呼叫员的编号
输出所有数据的拨号流水,并且在最后一行添加总呼叫次数
   记录呼叫员编号,对方号码,通话时长
 。。。。。。
总汇【市内号码总时长】【长途号码总时长】

 

create table T_phone(id int not null,
Caller nvarchar(20) not null,tonumber varchar(20) not null,
starttime datetime not null,endtime datetime not null)

insert into T_phone(caller,tonumber,starttime,endtime)
values('009','15358168859',getdate(),dateadd(ss,513,dateadd(mm,8,getdate())))

update T_phone set endtime =
dateadd(month,abs(datepart(month,endtime)-datepart(month,starttime)),endtime)
where datediff(month,starttime,endtime)<0


输出所有数据中通话时间最长的5条记录。
select top 5 *,datediff(second,starttime,endtime)as N'持续时间' from T_phone
order by datediff(second,starttime,endtime) DESC

输出所有数据中拨打长途号码(对方号码以5开头)的总时长
select sum(cast(datediff(second,starttime,endtime) as int))
from T_phone
where substring(tonumber,1,1)='5'
或者
where tonumber like '5%'

输出本月通话时长最多的前三个呼叫员的编号
select top 3 caller,sum(cast(datediff(second,starttime,endtime) as int))as N'总时长'
from T_phone
where datediff(year,starttime,getdate())=0 and
datediff(month,starttime,getdate())=0
group by caller
order by sum(cast(datediff(second,starttime,endtime) as int)) DESC

输出本月拨打电话次数最多的前2个呼叫员的编号
select top 2 caller,count(*)as N'次数'
from T_phone
where datediff(year,starttime,getdate())=0 and
datediff(month,starttime,getdate())=0
group by caller
order by count(*) DESC

   记录呼叫员编号,对方号码,通话时长
 。。。。。。
总汇【市内号码总时长】【长途号码总时长】
select
caller,
sum
(
case
when tonumber like '2%' then datediff(second,starttime,endtime)
else 0
end
)as N'长途',
sum
(
case
when tonumber not like '2%' then datediff(second,starttime,endtime)
else 0
end
)as N'市内'
from T_phone
group by caller
union
select
N'汇总',
sum
(
case
when tonumber like '2%' then datediff(second,starttime,endtime)
else 0
end
)as N'长途',
sum
(
case
when tonumber not like '2%' then datediff(second,starttime,endtime)
else 0
end
)as N'市内'
from T_phone

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值