欢迎投稿

今日深度:

SQL语句相关概念及练习之基础篇(1)(5)

  1. --题5)创建一张表,记录电话呼叫员的工作流水,记录呼叫员编号,对方号码,通话开始时间,通话结束时间,。 
  2. --创建一张表T_Callers,记录电话呼叫员的工作流水,记录呼叫员编号、对方号码、通话开始时间、通话结束时间。建表、插数据等最后都自己写SQL语句。 
  3. --要求: 
  4. --1)输出所有数据中通话时间最长的5条记录。 
  5. --2)输出所有数据中拨打长途号码对方号码以0开头)的总时长。 
  6. --3)输出本月通话总时长最多的前三个呼叫员的编号。 
  7.  
  8. --4)输出本月拨打电话次数最多的前三个呼叫员的编号。 
  9.  
  10. --5)输出所有数据的拨号流水,并且在最后一行添加总呼叫时长。 
  11.  
  12. --记录呼叫员编号、对方号码、通话时长 
  13.  
  14. --...... 
  15.  
  16. --汇总[市内号码总时长][长途号码总时长] 
  17.  
  18.  
  19. --IdCallerNumberTellNumberStartDateTimeEndDateTime 
  20.  
  21. --1001020888888882010-7-1010:012010-7-1010:05 
  22.  
  23. --2001020888888882010-7-1113:412010-7-1113:52 
  24.  
  25. --3001898989892010-7-1114:422010-7-1114:49 
  26.  
  27. --4002021883689812010-7-1321:042010-7-1321:18 
  28.  
  29. --5002767676762010-6-2920:152010-6-2920:30 
  30.  
  31. --6001022888782432010-7-1513:402010-7-1513:56 
  32.  
  33. --7003672546862010-7-1311:062010-7-1311:19 
  34.  
  35. --8003862314452010-6-1919:192010-6-1919:25 
  36.  
  37. --9001874223682010-6-1919:252010-6-1919:36 
  38.  
  39. --10004400458622452010-6-1919:502010-6-1919:59 
  40.  
  41. --创建表 
  42.  
  43. createtableT_CallRecords( 
  44.  
  45. idintnotnull, 
  46.  
  47. CallerNumbervarchar(3), 
  48.  
  49. TellNumbervarchar(13), 
  50.  
  51. StartDateTImedatetime, 
  52.  
  53. EndDateTimedatetime, 
  54.  
  55. Primarykey(Id) 
  56.  
  57. ); 
  58. --插入数据 
  59.  
  60. insertintoT_CallRecords(Id,CallerNumber,TellNumber,StartDateTime,EndDateTIme) 
  61.  
  62. values(1,'001','02088888888','2010-7-1010:01','2010-7-1010:05'); 
  63.  
  64. INSERTINTOT_CallRecords(Id,CallerNumber,TellNumber,StartDateTime,EndDateTime) 
  65.  
  66. VALUES(2,'002','02088888888','2010-7-1113:41','2010-7-1113:52'); 
  67.  
  68. INSERTINTOT_CallRecords(Id,CallerNumber,TellNumber,StartDateTime,EndDateTime) 
  69.  
  70. VALUES(3,'003','89898989','2010-7-1114:42','2010-7-1114:49'); 
  71.  
  72. INSERTINTOT_CallRecords(Id,CallerNumber,TellNumber,StartDateTime,EndDateTime) 
  73.  
  74. VALUES(4,'004','02188368981','2010-7-1321:04','2010-7-1321:18'); 
  75.  
  76. INSERTINTOT_CallRecords(Id,CallerNumber,TellNumber,StartDateTime,EndDateTime) 
  77.  
  78. VALUES(5,'005','76767676','2010-6-2920:15','2010-6-2920:30'); 
  79.  
  80. INSERTINTOT_CallRecords(Id,CallerNumber,TellNumber,StartDateTime,EndDateTime) 
  81.  
  82. VALUES(6,'006','02288878243','2010-7-1513:40','2010-7-1513:56'); 
  83.  
  84. INSERTINTOT_CallRecords(Id,CallerNumber,TellNumber,StartDateTime,EndDateTime) 
  85.  
  86. VALUES(7,'007','67254686','2010-7-1311:06','2010-7-1311:19'); 
  87.  
  88. INSERTINTOT_CallRecords(Id,CallerNumber,TellNumber,StartDateTime,EndDateTime) 
  89.  
  90. VALUES(8,'008','86231445','2010-6-1919:19','2010-6-1919:25'); 
  91.  
  92. INSERTINTOT_CallRecords(Id,CallerNumber,TellNumber,StartDateTime,EndDateTime) 
  93.  
  94. VALUES(9,'009','87422368','2010-6-1919:25','2010-6-1919:36'); 
  95.  
  96. INSERTINTOT_CallRecords(Id,CallerNumber,TellNumber,StartDateTime,EndDateTime) 
  97.  
  98. VALUES(10,'010','40045862245','2010-6-1919:50','2010-6-1919:59'); 
  99.  
  100. --修改呼叫员编号 
  101.  
  102. UPDATET_CallRecordsSETCallerNumber='001'WHEREIdIN(1,2,3,6,9); 
  103.  
  104. UPDATET_CallRecordsSETCallerNumber='002'WHEREIdIN(4,5); 
  105.  
  106. UPDATET_CallRecordsSETCallerNumber='003'WHEREIdIN(7,8); 
  107.  
  108. UPDATET_CallRecordsSETCallerNumber='004'WHEREId=10; 
  109.  
  110. --数据汇总 
  111.  
  112. select*fromT_CallRecords 
  113.  
  114.  
  115.  
  116. --题1):输出所有数据中通话时间最长的5条记录。 
  117.  
  118. --@计算通话时间; 
  119.  
  120. --@按通话时间降序排列; 
  121.  
  122. --@取前5条记录。 
  123.  
  124. selecttop5CallerNumber,DATEDIFF(SECOND,StartDateTime,EndDateTime)as总时长 
  125.  
  126. fromT_CallRecords 
  127.  
  128. orderbyDATEDIFF(SECOND,StartDateTime,EndDateTime)DESC 
  129.  
  130. --题2):输出所有数据中拨打长途号码对方号码以0开头)的总时长 
  131.  
  132. --@查询拨打长途号码的记录; 
  133.  
  134. --@计算各拨打长途号码的通话时长; 
  135.  
  136. --@对各拨打长途号码的通话时长进行求和。 
  137.  
  138. selectSUM(DATEDIFF(SECOND,StartDateTime,EndDateTime))as总时长fromT_CallRecords 
  139.  
  140. whereTellNumberlike'0%' 
  141.  
  142. --题3):输出本月通话总时长最多的前三个呼叫员的编号。 
  143.  
  144. --@按呼叫员编号进行分组; 
  145.  
  146. --@计算各呼叫员通话总时长; 
  147.  
  148. --@按通话总时长进行降序排列; 
  149.  
  150. --@查询前3条记录中呼叫员的编号。 
  151.  
  152. selectdatediff(month,convert(datetime,'2010-06-01'),convert(datetime,'2010-07-22'))--测试 
  153.  
  154. selectCallerNumber,TellNumber,datediff(month,StartDateTime,EndDateTime) 
  155.  
  156. fromT_CallRecords 
  157.  
  158. selecttop3CallerNumberfromT_CallRecords 
  159.  
  160. wheredatediff(month,StartDateTime,getdate())=12--一年前的 
  161.  
  162. groupbyCallerNumber 
  163.  
  164. orderbySUM(DATEDIFF(SECOND,StartDateTime,EndDateTime))DESC 


www.htsjk.Com true http://www.htsjk.com/shujukujc/18792.html NewsArticle --题5)创建一张表,记录电话呼叫员的工作流水,记录呼叫员编号,对方号码,通话开始时间,通话结束时间,。 --创建一张表T_Callers,记录电话呼叫员的...
评论暂时关闭