SQL – 11.练习

    • 创建一张表,记录电话呼叫员的工作流水,记录呼叫员编号\对方号码\通话开始时间\通话结束时间。建表插数据等最后都自己写SQL语句。

      • 要求:

        1. 输出所有数据中通话时间最长的5条记录。

          orderby datediff

        2. 输出所有数据中拨打长途号码(对方号码以0开头)的总时长。

          like、sum

        3. 输出本月通话总时长最多的前三个呼叫员的编号。

          datediff(month....),sum,order  by

        4. 输出本月拨打电话次数最多的前三个呼叫员的编号.

          group by,count(*)

        5. 输出所有数据的拨号流水,并且在最后一行添加总呼叫时长

        6. 呼叫员编号、对方号码、通话时长

        7. .......

        8. 汇总  [市内号码总时长][长途号码总时长]

   

1.创建表

 -- CallerNumber:呼叫员编号 TelNum:对方号码
 --StartDateTime:通话开始时间 EndDateTime:通话结束时间
CREATE TABLE [T_CallRecords1](
    [Id] [int] NOT NULL,
    [CallerNumber] [nvarchar](50),
    [TelNum] [varchar](50),
    [StartDateTime] [datetime] NULL,
    [EndDateTime] [datetime] NULL);

2.插入数据

INSERT [dbo].[T_CallRecords1] ([Id], [CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES (1, N'001', N'0208888888', CAST(0x00009DAF00A4CB80 AS DateTime), CAST(0x00009DAF00A62E94 AS DateTime));
INSERT [dbo].[T_CallRecords1] ([Id], [CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES (2, N'001', N'0208888888', CAST(0x00009DB000D63BC0 AS DateTime), CAST(0x00009DB000D68DC8 AS DateTime));
INSERT [dbo].[T_CallRecords1] ([Id], [CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES (3, N'001', N'89898989', CAST(0x00009DB000E85C60 AS DateTime), CAST(0x00009DB000E92F50 AS DateTime));
INSERT [dbo].[T_CallRecords1] ([Id], [CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES (4, N'002', N'98987676', CAST(0x00009DB2015BB7A0 AS DateTime), CAST(0x00009DB2015C4DA0 AS DateTime));
INSERT [dbo].[T_CallRecords1] ([Id], [CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES (5, N'002', N'02188839389', CAST(0x00009DA4014C9C70 AS DateTime), CAST(0x00009DA4014E0308 AS DateTime));
INSERT [dbo].[T_CallRecords1] ([Id], [CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES (6, N'001', N'767676766', CAST(0x00009DB400DAA0C0 AS DateTime), CAST(0x00009DB400DD5FE0 AS DateTime));
INSERT [dbo].[T_CallRecords1] ([Id], [CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES (7, N'003', N'0227864656', CAST(0x00009DB200B9AB40 AS DateTime), CAST(0x00009DB200B9FC1C AS DateTime));
INSERT [dbo].[T_CallRecords1] ([Id], [CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES (8, N'003', N'676765777', CAST(0x00009DB8014042B8 AS DateTime), CAST(0x00009DB80141804C AS DateTime));
INSERT [dbo].[T_CallRecords1] ([Id], [CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES (9, N'001', N'89977653', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime));
INSERT [dbo].[T_CallRecords1] ([Id], [CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES (10, N'004', N'400400400', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime));

image

3.问题解决:

(1)输出所有数据中通话时间最长的5条记录

select top 5 Id,CallerNumber,TelNum,StartDateTime ,EndDateTime,
(datediff(SECOND,[startdatetime],[EndDateTime]))as N'通话时间'
from dbo.T_CallRecords1 
order by datediff(SECOND,[startdatetime],[EndDateTime]) desc

image

(2)输出所有数据中拨打长途号码(对方号码以0开头)的总时长。

select SUM( datediff(SECOND,[startdatetime],[EndDateTime]))as N'通话时间总时长'
from dbo.T_CallRecords1 where  TelNum like '0%'

image

(3)输出本月通话总时长最多的前三个呼叫员的编号。

 

--本月时间
--(因为数据是2010年的,假设现在时间为2010年7月2日)
select CallerNumber,TelNum,DATEDIFF(MONTH,StartDateTime,CONVERT(datetime,'2010-7-2')) from
dbo.T_CallRecords1

 image

最后1列:0表示当月,1表示前1月

--当月的数据(2010年7月)
select CallerNumber,TelNum,DATEDIFF(MONTH,StartDateTime,CONVERT(datetime,'2010-7-2')) 
from dbo.T_CallRecords1 where DATEDIFF(MONTH,StartDateTime,CONVERT(datetime,'2010-7-2')) = 0

image

现在取的都是当月了

--输出本月通话总时长最多的前三个呼叫员的编号。
 select top 3 CallerNumber,sum(datediff(SECOND,[startdatetime],[EndDateTime])) as N'通话总时长' 
 from dbo.T_CallRecords1  
 where DATEDIFF(MONTH,StartDateTime,CONVERT(datetime,'2010-7-2')) = 0    --当月的数据
 group by CallerNumber                                                    --呼叫员编号分组
 order by sum(DATEDIFF(SECOND,StartDateTime,EndDateTime)) desc            --通话总时长排序

image

(4)输出本月拨打电话次数最多的前三个呼叫员的编号.

--注意本月假设是2010年7月2日
select top 3 CallerNumber,COUNT(*) as N'拨打次数' from dbo.T_CallRecords1
where DATEDIFF(MONTH,StartDateTime,CONVERT(datetime,'2010-7-2')) = 0
group by CallerNumber
order by count(*) desc

image

(5)

  • 输出所有数据的拨号流水,并且在最后一行添加总呼叫时长

  • 呼叫员编号、对方号码、通话时长

  • .......

  • 汇总 [市内号码总时长][长途号码总时长]

第1步,先求汇总

--汇总

select
( case when TelNum not like '0%' then DATEDIFF(SECOND,[startdatetime],[EndDateTime]) else 0 end ) as '市内通话', ( case when TelNum like '0%' then DATEDIFF(SECOND,[startdatetime],[EndDateTime]) else 0 end ) as '长途通话' from dbo.T_CallRecords1

 image

第2步,呼叫员编号、对方号码、通话时长

--呼叫员编号、对方号码、通话时长
select CallerNumber,TelNum,datediff(SECOND,[startdatetime],[EndDateTime]) as N'通话时长' 
from dbo.T_CallRecords1  

 image

第3步,用union all集合起来,注意convert(varchar(50)转换telnum,因为其中数据(02188839389)转成整数时溢出

select CallerNumber,TelNum,datediff(SECOND,[startdatetime],[EndDateTime]) as N'通话时长' 
 from dbo.T_CallRecords1  
 union all
select '汇总',
convert(varchar(50),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 dbo.T_CallRecords1

 image

原文地址:https://www.cnblogs.com/tangge/p/2642595.html