2014-11-05 83 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