top
Loading...
如何解決一個公司的數據庫筆試題目

自己在SQL Server中建立一個數據庫(或利用現有數據庫),再建立下表。

SQL Server中表test:













CREATE  TABLE  test(                 Code          smallint  NOT  NULL,                --學生編號                 Name          varchar(10)  NOT  NULL,          --學生姓名                 Lessons    varchar(1000)  NOT  NULL,      --課程名稱,每個名稱以“,”結束                 Score1      varchar(1000)  NOT  NULL,      --期中成績,每課程成績以“,”結束                 Score2      varchar(1000)  NOT  NULL,      --期末成績,每課程成績以“,”結束                  PRIMARY  KEY  (Code)  )  go

實例記錄數據:

insert  into  test  (Code,Name,Lessons,Score1,Score2)  values(20301,'張三','語文,數學  ,外語,','100,99,98,','97,96,95,')  go  insert  into  test  (Code,Name,Lessons,Score1,Score2)  values(20302,'李四','語文,數學  ,外語,','90,89,88,','87,86,85,')  go  insert  into  test  (Code,Name,Lessons,Score1,Score2)  values(20303,'王五','語文,數學  ,外語,','70,69,68,','67,66,65,')  go

屏幕顯示實例:

編號   姓名    期中    期末    期中    期末     期中    期末課程 20301   張三    100      97       99      96       98      95  語文 20302   李四     90      87       89      86       88      85      數學 20302   李四     90      87       89      86       88      85    外語 20303   王五     70      67       69      66       68      65

題目的難點在于科目是動態的

陶清網站已經有人用sql實現了

他說沒有用到游標只用了一個函數

函數已經給出

但后面的具體實現過程他沒有提供

還請csdn上的高手們賜教

謝謝

drop  table  test  go  drop  table  #t  go  CREATE  TABLE  test(                 Code          smallint  NOT  NULL,                --學生編號                 Name          varchar(10)  NOT  NULL,          --學生姓名                 Lessons    varchar(1000)  NOT  NULL,      --課程名稱,每個名稱以“,”結束                 Score1      varchar(1000)  NOT  NULL,      --期中成績,每課程成績以“,”結束                 Score2      varchar(1000)  NOT  NULL,      --期末成績,每課程成績以“,”結束                  PRIMARY  KEY  (Code)  )  go   insert  into  test  (Code,Name,Lessons,Score1,Score2)  values(20301,'張三','語文,數學  ,外語,','100,99,98,','97,96,95,')  go  insert  into  test  (Code,Name,Lessons,Score1,Score2)  values(20302,'李四','語文,數學  ,外語,','90,89,88,','87,86,85,')  go  insert  into  test  (Code,Name,Lessons,Score1,Score2)  values(20303,'王五','語文,數學  ,外語,','70,69,68,','67,66,65,')  go  /*

屏幕顯示實例:

編號   姓名    期中    期末    期中    期末     期中    期末課程 20301   張三    100      97       99      96       98      95  語文 20302   李四     90      87       89      86       88      85      數學 20302   李四     90      87       89      86       88      85    外語 20303   王五     70      67       69      66       68      65

*/

create  table  #t(                 Code          smallint  NOT  NULL,                --學生編號                 Name          varchar(10)  NOT  NULL,          --學生姓名                 Lessons    varchar(100)  NOT  NULL,      --課程名稱,每個名稱以“,”結束                 Score1      int,                 Score2      int  )  go  Select  1  while  @@rowcount  >  0  begin             insert  #t               select  code,name             ,left(lessons,charindex(',',lessons)-1)               ,left(score1,charindex(',',score1)-1)             ,left(score2,charindex(',',score2)-1)             from  test             where  charindex(',',lessons)>0              update  test  set  lessons  =  right(lessons,len(lessons)-charindex(',',lessons)),               score1  =  right(score1,len(score1)-charindex(',',score1))  ,             score2  =  right(score2,len(score2)-charindex(',',score2))               where  charindex(',',lessons)>0  end    declare  @sql  varchar(8000)  set  @sql  =  'select  code,name'  select  @sql  =  @sql  +  ',sum(case  lessons  when  '''+lessons+'''  then  cast(score1  as  int)  end)  ['+rtrim(lessons)+'中]'  +  ',sum(case  lessons  when  '''+lessons+'''  then  cast(score2  as  int)  end)  ['+rtrim(lessons)+'末]'   from  (select  distinct  lessons  as  lessons  from  #t)  as  a  select  @sql  =  @sql+'  from  #t  group  by  code,name'  exec(@sql)    code    name  數學中    數學末    外語中    外語末    語文中   語文末              20302    李四   89        86       88         85        90       87  20303    王五   69        66       68         65        70       67  20301    張三   99        96       98         95       100       97
作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗