开通VIP,畅享免费电子书等14项超值服
首页
好书
留言交流
下载APP
联系客服
2013.03.06
-- 创建一张表,记录电话呼叫员的工作流水,记录呼叫员编号 Id 、对方号码 CallerNumber、-- 通话开始时间 StartDateTime、通话结束时间 EndDateTimer 。-- 建表、插数据等最后都自己写 SQL 语句。Create database CallCenteron --总是忘记 on( Name ='CallCenter' , filename ='c:\abc\CallCenter.mdf' , size = 3 , maxsize =100 , filegrowth =10)use CallCentergocreate table [CallRecords]( [Id] int identity( 1 ,1 ) not null, [CallerNumber] nvarchar ( 50), [TelNumber] nvarchar ( 30), [StartDateTime] datetime null, [EndDateTime] datetime null)-- 添加约束-- 主键alter table CallRecordsadd constraint PK_CallRecords_Id primary key ( Id)-- 检查约束--enddatetime 必须晚于startdatetimealter table CallRecordsadd constraint CK_CallRecords_EndStart check (EndDateTime > StartDateTime)--telNumber 类似于alter table CallRecordsadd constraint CK_CallRecords_TelNumber check (TelNumber like '[0-9][0-9][0-9]')--alter table CallRecords 删除约束--drop constraint CK_CallRecords_TelNumber-- 默认结束时间为当前时间alter table CallRecordsadd constraint DF_CallRecords_EndDateTime default (getdate ()) for EndDateTimeINSERT [dbo] . [CallRecords] ( [CallerNumber], [TelNumber], [StartDateTime] , [EndDateTime] ) VALUES ('001' ,'0208888888' , CAST (0x00009DAF00A4CB80 AS DateTime ), CAST ( 0x00009DAF00A62E94 AS DateTime ));INSERT [dbo] . [CallRecords] ( [CallerNumber], [TelNumber], [StartDateTime] , [EndDateTime] ) VALUES ('001' ,'0208888888' , CAST (0x00009DB000D63BC0 AS DateTime ), CAST ( 0x00009DB000D68DC8 AS DateTime ));INSERT [dbo] . [CallRecords] ( [CallerNumber], [TelNumber], [StartDateTime] , [EndDateTime] ) VALUES ('001' ,'89898989' , CAST (0x00009DB000E85C60 AS DateTime ), CAST ( 0x00009DB000E92F50 AS DateTime ));INSERT [dbo] . [CallRecords] ( [CallerNumber], [TelNumber], [StartDateTime] , [EndDateTime] ) VALUES ('002' ,'98987676' , CAST (0x00009DB2015BB7A0 AS DateTime ), CAST ( 0x00009DB2015C4DA0 AS DateTime ));INSERT [dbo] . [CallRecords] ( [CallerNumber], [TelNumber], [StartDateTime] , [EndDateTime] ) VALUES ('002' ,'02188839389' , CAST (0x00009DA4014C9C70 AS DateTime ), CAST ( 0x00009DA4014E0308 AS DateTime ));INSERT [dbo] . [CallRecords] ( [CallerNumber], [TelNumber], [StartDateTime] , [EndDateTime] ) VALUES ('001' ,'767676766' , CAST (0x00009DB400DAA0C0 AS DateTime ), CAST ( 0x00009DB400DD5FE0 AS DateTime ));INSERT [dbo] . [CallRecords] ( [CallerNumber], [TelNumber], [StartDateTime] , [EndDateTime] ) VALUES ('003' ,'0227864656' , CAST (0x00009DB200B9AB40 AS DateTime ), CAST ( 0x00009DB200B9FC1C AS DateTime ));INSERT [dbo] . [CallRecords] ( [CallerNumber], [TelNumber], [StartDateTime] , [EndDateTime] ) VALUES ('003' ,'676765777' , CAST (0x00009DB8014042B8 AS DateTime ), CAST ( 0x00009DB80141804C AS DateTime ));INSERT [dbo] . [CallRecords] ( [CallerNumber], [TelNumber], [StartDateTime] , [EndDateTime] ) VALUES ('001' ,'89977653' , CAST (0x00009D9A00FB9898 AS DateTime ), CAST ( 0x00009D9A00FE6118 AS DateTime ));INSERT [dbo] . [CallRecords] ( [CallerNumber], [TelNumber], [StartDateTime] , [EndDateTime] ) VALUES ('004' ,'400400400' , CAST (0x00009D9A00FB9898 AS DateTime ), CAST ( 0x00009D9A00FE6118 AS DateTime ));-- 输出所有数据中通话时间最长的条记录。 orderby datediffselect top 5 * from CallRecordsorder by DATEDIFF( SECOND ,StartDateTime , EndDateTime) desc-- 输出所有数据中拨打长途号码(对方号码以开头)的总时长。 like 、sumselect SUM ( DATEDIFF( second ,StartDateTime , EndDateTime)) from CallRecordswhere TelNumber like'0%'-- 输出本月通话总时长最多的前三个呼叫员的编号。select top 3 callernumber from CallRecords --错误-- 正确select top 3 sum( DATEDIFF (second , StartDateTime, EndDateTime ))as '本月总时长 ' , CallerNumber as '呼叫员编号 ' fromCallRecordswhere datediff ( month, StartDateTime ,getdate ())= 0group by CallerNumberorder by sum( DATEDIFF (second , StartDateTime, EndDateTime ))desc-- 查询本月通话记录select * from CallRecords where datediff (month , StartDateTime, getdate ())=0-- 输出本月拨打电话次数最多的前三个呼叫员的编号 group by,count(*)select top 3 COUNT(*), CallerNumber from CallRecordswhere datediff ( month, StartDateTime ,getdate ())= 0group by CallerNumberorder by COUNT(*) desc5、补充use MySchoolselect * from class--output 就是输出inserted 就是零时存储插入数据产生的新 Idinsert into Class output inserted . cID values ('bcd' , 'bcd')-- 还可以使用 @@indentity返回新产生的 id 但是下面语句写在 vs中时需要在 select 前添加分号否则报错insert into Class values ('xyz' , 'xyz') select @@IDENTITY--sysobjects 从系统表中查找是否存在 User 表if exists( select * from sysobjects where name= 'user' ) drop table [user]gocreate table [user]( uId int identity( 1 ,1 ) primary key, uUserName nvarchar ( 20) not null, uPwd varchar ( 10), uTimes int --)go--user 是SQlServer 关键字 所以加 [] 区分insert into [user] ( uUserName, uPwd ,uTimes ) values ( 'admin', '123456' ,0 )insert into [user] ( uUserName, uPwd ,uTimes ) values ( 'user', '123456' ,0 )select * from [user]truncate table [user]insert into [user] ( uusername, upwd )select '张三 ' ,'00000' unionselect '李四 ' ,'00000' unionselect '王五 ' ,'00000' unionselect '赵六 ' ,'00000' unionselect '王八 ' ,'00000'
微信登录中...请勿关闭此页面