0
我有錯誤類型,錯誤代碼和錯誤計數的表:SQL樞軸和加入
#ErrorCounts
ErrorCountID CourseID ErrorCodeID ErrorTypeID ErrorCount
1 1 1 1 10
2 1 2 1 4
3 1 3 2 5
#ErrorTypes
ErrorTypeID Description
1 'Direction'
2 'Generic'
3 'Information'
--And then 3 tables containing descriptions of all these errors types
#ErrorDirectionCodes
CodeID Description
1 'You are moving in the wrong direction'
2 'You are in the right direction'
#ErrorGenericCodes
CodeID Description
1 'Generic Error'
2 'Generic Message'
#ErrorInformationCodes
CodeID Description
1 'Wrong information'
2 'Typo information'
--And lastly a table for the courses
#Courses
CourseID UserID
1 10
2 11
--etc
我需要顯示在每場失誤的數量的報告,我得到這個:
SELECT
CASE
WHEN ErrorTypeID = 1 THEN 'Description Error'
WHEN ErrorTypeID = 2 THEN 'Generic Error'
WHEN ErrorTypeID = 3 THEN 'Information Error'
ELSE 'Unknown Error Type'
END AS ErrorType,
EC.ErrorCodeID,
COALESCE(
EDC.Description,
EGC.Description,
EIC.Description,
'Unknown Error Type'
) AS ErrorDescription
FROM ErrorCount EC
LEFT JOIN ErrorDirectionCodes AS EDC ON EDC.CodeID = EC.ErrorCodeID AND EC.ErrorTypeID = 1
LEFT JOIN ErrorGenericCodes AS EGC ON EGC.CodeID = EC.ErrorCodeID AND EC.ErrorTypeID = 2
LEFT JOIN ErrorInformationCodes AS EIC ON EIC.CodeID = EC.ErrorCodeID AND EC.ErrorTypeID = 3
GROUP BY EC.ErrorCodeID, EC.ErrorTypeID, EDC.Description, EGC.Description, EIC.Description
哪些羣體通過自己的錯誤類型,但問題是下一個部分,在那裏我有顯示爲列,發生這些錯誤的課程,像這樣:
ErrorType ErrorCode ErrorDescription Course1 Course2
1 1 'You are moving in the wrong direction' 10 0
2 1 'Generic Error' 5 2
--etc
我讀了如何PIVOT
的作品,但我仍然無法得到它的工作,我不知道怎麼下JOIN雲:
INNER JOIN Courses C ON C.CourseID = EC.CourseID
PIVOT (
?
)
我怎樣才能得到這個工作PIVOT?
之前,你甚至得到你樞軸轉動誤差串的電流查詢似乎關閉。重複#ErrorCounts並更改courseid並保留ErrorCodeID ErrorTypeID。你會看到你得到一個記錄有多個錯誤代碼,不同的描述,但相同的錯誤類型和錯誤代碼。如果你嘗試加入,這會讓你變得非常笨拙。 – TYY 2014-11-05 21:34:56