SQL 数据小练习,呼叫中心 ----查询通话时间最长的5条记录 ----查询以0开头的通话总时,以秒为计算单位 ----查询2010年7月通话总时长最多的前两个呼叫员的编号 ----查询201
2016-09-21 14:31
591 查看
--CREATE TABLE [CallRecords]
--(
-- [Id] [int] NOT NULL identity(1,1),
-- [CallerNumber] [nvarchar](50), --三位数字,呼叫中心员工编号(工号)
-- [TelNum] [varchar](50),
-- [StartDateTime] [datetime] NULL,
-- [EndDateTime] [datetime] NULL --结束时间要大于开始时间,默认当前时间
--)
----主键约束
--alter table [CallRecords]
--add constraint PK_CallRecords primary key(id)
----检查约束
--alter table [CallRecords]
--add constraint CK_CallRecords check(CallerNumber like '[0-9][0-9][0-9]')
--alter table [CallRecords]
--add constraint CK_CallRecords_EndDateTime check(EndDateTime > StartDateTime)
----默认约束
--alter table [CallRecords]
--add constraint DF_CallRecords default(getdate()) for EndDateTime
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '0208888888', CAST(0x00009DAF00A4CB80 AS DateTime), CAST(0x00009DAF00A62E94 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '0208888888', CAST(0x00009DB000D63BC0 AS DateTime), CAST(0x00009DB000D68DC8 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '89898989', CAST(0x00009DB000E85C60 AS DateTime), CAST(0x00009DB000E92F50 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('002', '98987676', CAST(0x00009DB2015BB7A0 AS DateTime), CAST(0x00009DB2015C4DA0 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('002', '02188839389', CAST(0x00009DA4014C9C70 AS DateTime), CAST(0x00009DA4014E0308 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '767676766', CAST(0x00009DB400DAA0C0 AS DateTime), CAST(0x00009DB400DD5FE0 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('003', '0227864656', CAST(0x00009DB200B9AB40 AS DateTime), CAST(0x00009DB200B9FC1C AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('003', '676765777', CAST(0x00009DB8014042B8 AS DateTime), CAST(0x00009DB80141804C AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '89977653', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('004', '400400400', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime));
select * from dbo.CallRecords
----查询通话时间最长的5条记录
use test
go
select top 5 Id,CallerNumber,DATEDIFF
(second,startdatetime,enddatetime) as dutime
from CallRecords
order by dutime desc
----查询以0开头的通话总时,以秒为计算单位
select
SUM(datediff(second,startdatetime,enddatetime))
from CallRecords
where TelNum like '0%'
----查询2010年7月通话总时长最多的前两个呼叫员的编号
select
top 2
CallerNumber,
sum(datediff(second,startdatetime,enddatetime)) as 时长,
count(*) as 电话数
from CallRecords
where DATEDIFF(MONTH,'2010-7-1',startdatetime)=0
group by CallerNumber
order by 时长 desc
----查询2010年7月拨打电话次数最多的前两个呼叫员的编号
select
top 2
CallerNumber,
sum(datediff(second,startdatetime,enddatetime)) as 时长,
count(*) as 电话数
from CallRecords
where DATEDIFF(MONTH,'2010-7-1',startdatetime)=0
group by CallerNumber
order by 电话数 desc
--(
-- [Id] [int] NOT NULL identity(1,1),
-- [CallerNumber] [nvarchar](50), --三位数字,呼叫中心员工编号(工号)
-- [TelNum] [varchar](50),
-- [StartDateTime] [datetime] NULL,
-- [EndDateTime] [datetime] NULL --结束时间要大于开始时间,默认当前时间
--)
----主键约束
--alter table [CallRecords]
--add constraint PK_CallRecords primary key(id)
----检查约束
--alter table [CallRecords]
--add constraint CK_CallRecords check(CallerNumber like '[0-9][0-9][0-9]')
--alter table [CallRecords]
--add constraint CK_CallRecords_EndDateTime check(EndDateTime > StartDateTime)
----默认约束
--alter table [CallRecords]
--add constraint DF_CallRecords default(getdate()) for EndDateTime
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '0208888888', CAST(0x00009DAF00A4CB80 AS DateTime), CAST(0x00009DAF00A62E94 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '0208888888', CAST(0x00009DB000D63BC0 AS DateTime), CAST(0x00009DB000D68DC8 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '89898989', CAST(0x00009DB000E85C60 AS DateTime), CAST(0x00009DB000E92F50 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('002', '98987676', CAST(0x00009DB2015BB7A0 AS DateTime), CAST(0x00009DB2015C4DA0 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('002', '02188839389', CAST(0x00009DA4014C9C70 AS DateTime), CAST(0x00009DA4014E0308 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '767676766', CAST(0x00009DB400DAA0C0 AS DateTime), CAST(0x00009DB400DD5FE0 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('003', '0227864656', CAST(0x00009DB200B9AB40 AS DateTime), CAST(0x00009DB200B9FC1C AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('003', '676765777', CAST(0x00009DB8014042B8 AS DateTime), CAST(0x00009DB80141804C AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '89977653', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime));
--INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('004', '400400400', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime));
select * from dbo.CallRecords
----查询通话时间最长的5条记录
use test
go
select top 5 Id,CallerNumber,DATEDIFF
(second,startdatetime,enddatetime) as dutime
from CallRecords
order by dutime desc
----查询以0开头的通话总时,以秒为计算单位
select
SUM(datediff(second,startdatetime,enddatetime))
from CallRecords
where TelNum like '0%'
----查询2010年7月通话总时长最多的前两个呼叫员的编号
select
top 2
CallerNumber,
sum(datediff(second,startdatetime,enddatetime)) as 时长,
count(*) as 电话数
from CallRecords
where DATEDIFF(MONTH,'2010-7-1',startdatetime)=0
group by CallerNumber
order by 时长 desc
----查询2010年7月拨打电话次数最多的前两个呼叫员的编号
select
top 2
CallerNumber,
sum(datediff(second,startdatetime,enddatetime)) as 时长,
count(*) as 电话数
from CallRecords
where DATEDIFF(MONTH,'2010-7-1',startdatetime)=0
group by CallerNumber
order by 电话数 desc
相关文章推荐
- sqlplus查询数据中的最新一条,表中有记录时间的字段
- mysql的sql语句中直接计算时间 查询昨天 一周前 一月前 一年前的数据的方法
- Spark项目练习(计算用户停留时间最长的两个小区)
- mysql的sql语句中直接计算时间 查询昨天 一周前 一月前 一年前的数据的方法
- sqlserver 触发器/计算两个时间的间隔天数/查询倒数第二条数据
- sql根据时间查询记录
- 使用SQL语句对重复记录查询、统计重复次数、删除重复数据
- SQL语句:用一条语句找到最早的时间和最近的时间两个记录(在规定时间段内)
- csdn摘录-sql 执行时间,sql导出查询数据到Excel...
- sql 按递增查询记录,并获取已排序的数据的百分比
- 在数据表中用sql实现累计计算查询(sql server 2000)
- oracle 计算两个时间的分钟差 sql语句
- SQL中获取指定两个日期时间点或者时间段内所有记录语句
- SQL中获取指定两个日期时间点或者时间段内所有记录语句
- 查询前10条记录,取后面5条记录的sql代码
- 查询分组计算[按时间分组计算][SQL]
- 从统计数据表记录总数谈SQL_Server查询优化
- [VB.NET]用SQL查询得到(按编号分组的日期最大的记录)
- (转)MySQL 查询重复出现次数最多的记录,按出现频率排序(SQL语句)
- MS SQL Server 表数据更新时间查询