2014-11-05 106 views
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?

+0

之前,你甚至得到你樞軸轉動誤差串的電流查詢似乎關閉。重複#ErrorCounts並更改courseid並保留ErrorCodeID ErrorTypeID。你會看到你得到一個記錄有多個錯誤代碼,不同的描述,但相同的錯誤類型和錯誤代碼。如果你嘗試加入,這會讓你變得非常笨拙。 – TYY 2014-11-05 21:34:56

回答

0

我給出了一個數據透視的例子。因爲我不知道課程的含義,所以我把隨機的課程名稱。

declare @ErrorCount table 
(
    ErrorCountID int, 
    CourseID int, 
    ErrorCodeID int, 
    ErrorTypeID int, 
    ErrorCount int) 

insert into @ErrorCount 
values 
(1    ,1   ,1    ,1    ,10), 
(2    ,1   ,2    ,1    ,4), 
(3    ,1   ,3    ,2    ,5) 


declare @ErrorTypes table 
(
    ErrorTypeID  int, 
    Description  Varchar(20) 
) 

insert into @ErrorTypes 
values 
(1,    'Direction'), 
(2,    'Generic'), 
(3,    'Information') 

--And then 3 tables containing descriptions of all these errors types 
declare @ErrorDirectionCodes table 
(
    CodeID   int, 
    Description  varchar(256) 
) 

insert into @ErrorDirectionCodes 
values 
(1    ,'You are moving in the wrong direction'), 
(2    ,'You are in the right direction') 


declare @ErrorGenericCodes table 
(
    CodeID   int, 
    Description  varchar(56) 
) 

insert into @ErrorTypes 
values 
(1,    'Generic Error'), 
(2,    'Generic Message') 

declare @ErrorInformationCodes table 
(
    CodeID   int, 
    Description  varchar(56) 
) 

insert into @ErrorInformationCodes 
values 
(1,    'Wrong information'), 
(2,    'Typo information') 


--And lastly a table for the courses 
declare @Courses table 
(
    CourseID   int, 
    UserID  int, 
    CourseName Varchar(20) 
) 
insert into @Courses 
values 
(1    ,10, 'Forward'), 
(2    ,11, 'Backwards') 


SELECT ErrorCodeID,ErrorType, ErrorCodeID,ErrorDescription,ISNULL(Forward,0) as Foward,ISNULL(Backwards, 0) as Backwards 
FROM 
(
    SELECT c.CourseName,EC.ErrorType, EC.ErrorCodeID, EC.ErrorDescription, e.ErrorCount 
    FROM 
    (
     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 
    ) EC 
    INNER JOIN @ErrorCount e ON e.ErrorCodeID=EC.ErrorCodeID 
    INNER JOIN @Courses c ON c.CourseID = e.CourseID 
    ) v 
PIVOT 
(
    SUM(ErrorCount) 
    FOR CourseName IN ([Backwards],[Forward]) 
) c 

enter image description here