表结构如下:
相关查询:
--所有数据中通话时间最长的5条记录
select top 5 * from CallRecords1
order by dateDiff(second,StartDateTime,EndDateTime) desc
--输出所有数据中拨打长途号码(对方号码以0开头)的总时长。like,sum
select sum(dateDiff(second,StartDateTime,EndDateTime)) from CallRecords1
where TelNum like '0%'
select top 5 * from CallRecords1
order by dateDiff(second,StartDateTime,EndDateTime) desc
--输出所有数据中拨打长途号码(对方号码以0开头)的总时长。like,sum
select sum(dateDiff(second,StartDateTime,EndDateTime)) from CallRecords1
where TelNum like '0%'
--输出本月拨打电话次数最多的前三个呼叫员的编号
select * from CallRecords1
--分组出,每个呼叫员打的电话次数
select CallerNumber,count(*) from CallRecords1
group by CallerNumber
--根据每个呼叫员打的电话次数进行排序,然后取前三
select top 3 CallerNumber,count(*) as countsNumber from CallRecords1
group by CallerNumber order by countsNumber desc
select * from CallRecords1
--分组出,每个呼叫员打的电话次数
select CallerNumber,count(*) from CallRecords1
group by CallerNumber
--根据每个呼叫员打的电话次数进行排序,然后取前三
select top 3 CallerNumber,count(*) as countsNumber from CallRecords1
group by CallerNumber order by countsNumber desc
--汇总(市内号码总时长)(长途号码总时长)
select * from CallRecords1
--市内号码,TelNum开头不带0,的每个拨打时长秒数
select datediff(second,StartDateTime,EndDateTime) from CallRecords1
where TelNum not like '0%'
--进行汇总
select sum(datediff(second,StartDateTime,EndDateTime)) as 市内号码
from CallRecords1
where TelNum not like '0%'
--对长途号码,也如下统计
select sum(datediff(second,StartDateTime,EndDateTime)) as 长途
from CallRecords1
where TelNum like '0%'
--两者结合在一起
select (
select sum(datediff(second,StartDateTime,EndDateTime)) as 市内号码
from CallRecords1
where TelNum not like '0%'
) as 市内号码,
(
select sum(datediff(second,StartDateTime,EndDateTime)) as 长途
from CallRecords1
where TelNum like '0%'
)as 长途号码
--第二种方法汇总
select sum((
case
when TelNum not like '0%' then datediff(second,StartDateTime,EndDateTime)
else 0
end
))as 市内号码,
sum((
case
when TelNum like '0%' then datediff(second,StartDateTime,EndDateTime)
else 0
end
))as 长途号码
from CallRecords1
select * from CallRecords1
--市内号码,TelNum开头不带0,的每个拨打时长秒数
select datediff(second,StartDateTime,EndDateTime) from CallRecords1
where TelNum not like '0%'
--进行汇总
select sum(datediff(second,StartDateTime,EndDateTime)) as 市内号码
from CallRecords1
where TelNum not like '0%'
--对长途号码,也如下统计
select sum(datediff(second,StartDateTime,EndDateTime)) as 长途
from CallRecords1
where TelNum like '0%'
--两者结合在一起
select (
select sum(datediff(second,StartDateTime,EndDateTime)) as 市内号码
from CallRecords1
where TelNum not like '0%'
) as 市内号码,
(
select sum(datediff(second,StartDateTime,EndDateTime)) as 长途
from CallRecords1
where TelNum like '0%'
)as 长途号码
--第二种方法汇总
select sum((
case
when TelNum not like '0%' then datediff(second,StartDateTime,EndDateTime)
else 0
end
))as 市内号码,
sum((
case
when TelNum like '0%' then datediff(second,StartDateTime,EndDateTime)
else 0
end
))as 长途号码
from CallRecords1
--呼叫员编号,对方号码,通话时长,加汇总
select CallerNumber,TelNum,datediff(second,StartDateTime,EndDateTime) from CallRecords1
union all
select N'汇总',(
select convert(varchar(50),sum(datediff(second,StartDateTime,EndDateTime)))
from CallRecords1
where TelNum not like '0%'
),
(
select sum(datediff(second,StartDateTime,EndDateTime))
from CallRecords1
where TelNum like '0%'
)
--如果没进行convert转换,你看下查询结果,思考有啥不同 select CallerNumber,TelNum,datediff(second,StartDateTime,EndDateTime) from CallRecords1
union all
select N'汇总',(
select convert(varchar(50),sum(datediff(second,StartDateTime,EndDateTime)))
from CallRecords1
where TelNum not like '0%'
),
(
select sum(datediff(second,StartDateTime,EndDateTime))
from CallRecords1
where TelNum like '0%'
)