(1)输出所有数据中通话时间最长的5条记录。orderby
datediff
select top 5*,DATEDIFF(ss,StartDateTime,EndDateTime) as '通话时间(秒)' from CallRecords order by DATEDIFF(ss,StartDateTime,EndDateTime) desc;
---------------------
(2)输出所有数据中拨打长途号码(对方号码以0开头
)的总时长。like、sum
select count(*)as'符合条件',sum(DATEDIFF(ss,StartDateTime,EndDateTime))as '总时长' from CallRecords where TelNum like '0%';
---------------------
(3)输出本月通话总时长最多的前三个呼叫员的编号。
select top 3 CallerNumber as '编号',sum(datediff(ss,StartDateTime,EndDateTime))
from CallRecords
where datepart(mm,StartDateTime)=datepart(mm,getdate())
group by CallerNumber
order by sum(datediff(ss,StartDateTime,EndDateTime)) desc
---------------------
(4)输出本月拨打电话次数最多的前三个呼叫员的编号
.group by,count(*)按照月份分组。
select top 3 CallerNumber,count(*)
from CallRecords
where datepart(mm,StartDateTime)=datepart(mm,getdate())
group by CallerNumber
order by count(*) desc