2015-04-30 112 views
0

我有一組數據表的,看起來像這樣:轉列轉換爲行SQL

[id],[testid],[date],[type],[score1],[score2],[score3],[score4] 

我需要它看起來像這樣

[id],[testid][date],[type],[score],[scorecode] (score1) 
[id],[testid][date],[type],[score],[scorecode] (score2) 
[id],[testid][date],[type],[score],[scorecode] (score3) 
[id],[testid][date],[type],[score],[scorecode] (score4) 

的scorecode取決於其得分( 1-4),因此score1行需要ACTMATH的分數代碼,score2需要不同的分數代碼。

當我建立這個,我用UNION ALL。但是,我只是想確保沒有更有效的方法來完成這一點。

回答

1

你應該打出來的成績變成自己table

它有一個ScoreCode和得分和一個鏈接回該ID的測試

然後,您將執行SQL Join查詢,並輕鬆以該格式獲得您的結果。

測試 [testid],[日期],[類型]
比分 [ScoreId],[testid],[得分],[scorecode]

SELECT * 
FROM Tests T 
JOIN Scores S 
ON T.Testid = S.TestId