SQL Pivot和Join

时间:2014-11-05 20:09:02

标签: sql-server

我有一个错误类型,错误代码和错误计数表:

#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正常工作?

1 个答案:

答案 0 :(得分: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