交叉表的簡單實現1:使用預存程序

來源:互聯網
上載者:User

有位朋友要求幫忙實現交叉表(將某些行變為列),之前雖然實現過,但沒有整理,今天順便整理一下,便於自己以後參考,希望對其他網友有協助,歡迎指出不足之處^_^

資料庫中未經處理資料類似:

要求前台顯示為:


建立表結構: CREATE   TABLE   [ dbo ] . [ t_Score ]  (
     [ ScoreId ]   [ int ]   IDENTITY  ( 1 ,  1 )  NOT   NULL  ,
     [ SubjectName ]   [ varchar ]  ( 50 )  NOT   NULL  ,
     [ StudentName ]   [ varchar ]  ( 10 )  NOT   NULL  ,
     [ ScoreValue ]   [ real ]   NOT   NULL  ,
     [ ExamDate ]   [ datetime ]   NOT   NULL  
)

樣本資料
INSERT   INTO  t_Score(SubjectName, StudentName, ScoreValue, ExamDate)
VALUES ( ' C語言 ' ,  ' 孫光 ' ,  80 ,  ' 2006-01-05 ' )

INSERT   INTO  t_Score(SubjectName, StudentName, ScoreValue, ExamDate)
VALUES ( ' 日語 ' ,  ' 孫光 ' ,  79 ,  ' 2006-07-06 ' )

INSERT   INTO  t_Score(SubjectName, StudentName, ScoreValue, ExamDate)
VALUES ( ' C語言 ' ,  ' 孫光 ' ,  89 ,  ' 2006-08-09 ' )

INSERT   INTO  t_Score(SubjectName, StudentName, ScoreValue, ExamDate)
VALUES ( ' 英語 ' ,  ' 王二 ' ,  77 ,  ' 2006-09-10 ' )

INSERT   INTO  t_Score(SubjectName, StudentName, ScoreValue, ExamDate)
VALUES ( ' 英語 ' ,  ' 孫光 ' ,  77 ,  ' 2006-07-06 ' )

INSERT   INTO  t_Score(SubjectName, StudentName, ScoreValue, ExamDate)
VALUES ( ' C語言 ' ,  ' 王二 ' ,  89 ,  ' 2006-08-09 ' )


1。通過預存程序實現
實現要點:
a. 使用一個暫存資料表(#ScoreTbl)儲存指定學生的所有成績(根據實際情況,可能有更多條件)
說明:這裡無法使用表變數,因為表變數無法用於動態SQL語句中。

b.暫存資料表#ScoreTbl中使用一個平均值標誌位,因為將每個科目的平均值計算出來之後也存入了#ScoreTbl
當然,根據需求,還可以增加總成績標誌位等等

c.使用遞迴的SELECT語句建立動態 DateExam(考試時間)列(參考:Paul Nielsen 的 Microsoft SQL Server 2000寶典 P353 12-7 遞迴的Select變數)
注意:這裡遞迴成的 @sql  語句有個潛在的bug,就是@sql長度必須小於8000個字元(如果使用sq_executeSql執行動態語句,必須聲名為nvarchar,則只能使用4000個字元)
鄒建大哥的 化解字串不能超過8000的方法及交叉表的處理 討論了三種可選擇的方案。
本樣本不做此討論,只求實現交叉表^_^

d.使用CASE運算式選擇 DateExam 對應的 ScoreValue(成績值)(參考:Paul Nielsen 的 Microsoft SQL Server 2000寶典 P353 12-7 -2 動態交叉資料表查詢)

e.使用彙總函式(這裡使用SUM)包含ScoreValue列,因為ScoreValue未出現在GROUP BY子句中


具體實現: ALTER   PROC  usp_GetCrossScore4(
@StuName   varchar ( 10 )
)
AS
--  creates a temp table to hold the score records
CREATE   TABLE  #ScoreTbl(
ScoreId  int ,
SubjectName  varchar ( 50 ),
StudentName  varchar ( 10 ),
ScoreValue  real ,
ExamDate  datetime ,
AvgFlag  bit   DEFAULT ( 0 )     --  marks as the average of some subject
)
--  populates basic data of some student
INSERT   INTO  #ScoreTbl(ScoreId, SubjectName, StudentName, ScoreValue, ExamDate)
SELECT  ScoreId, SubjectName, StudentName, ScoreValue, ExamDate  FROM  t_Score s  WHERE  s.StudentName  =   @StuName
--  calculates total for per subject and appends to the temp table
INSERT   INTO  #ScoreTbl(ScoreId, SubjectName, StudentName, ScoreValue, ExamDate, AvgFlag)
SELECT   NULL , SubjectName, StudentName,  AVG (ScoreValue),  NULL ,  1   FROM  #ScoreTbl s  GROUP   BY  SubjectName, StudentName
-- SELECT * FROM #ScoreTbl

DECLARE   @Sql   varchar ( 8000 )
--  NOTE: some known bug -> you make sure the length of the dynamical sql is less than 8000.
--     fortunately, Mr Zou have made a deep discussion againt it at http://blog.csdn.net/zjcxc/archive/2003/12/29/20075.aspx.
SET   @Sql   =   ' SELECT SubjectName 科目 '
SELECT   @sql   =   @sql   +   ' , SUM(CASE ExamDate WHEN  '''   +   CONVERT ( varchar ( 20 ), ExamDate,  102 ) 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.