在课程名称

时间:2017-10-04 13:05:28

标签: sql tsql pivot

我希望有人可以帮助我。

在我们的数据库中,我们有一个包含所有用户的用户表。每个用户都可以完成多个培训课程。我的任务是:

a。)查找每个用户每个培训课程的最新完成日期

b。)转向培训课程,以便每个用户有一条记录,每个培训课程都有一列,其中包含最近的完成日期。

我遇到的问题是它仍然会为每个用户返回多行,而我无法理解为什么会这样。

这是我的T-SQL脚本:

SELECT userForeName,
       userSurname,
       CASE
           WHEN [Anti-Fraud] IS NULL
           THEN 'Course not started or incomplete'
           ELSE CONVERT(VARCHAR(255), [Anti-Fraud], 121)
       END,
       CASE
           WHEN [Approved Assessors and UNIQUS] IS NULL
           THEN 'Course not started or incomplete'
           ELSE CONVERT(VARCHAR(255), [Approved Assessors and UNIQUS], 121)
       END
FROM
(
    SELECT u.Forenames AS [userForeName],
           u.Surname AS [userSurname],
           m.Name AS [moduleName],
           tt.Name AS [courseName],
           ut.completed AS [lastCompleted]
    FROM radar.[User] u
         JOIN training.UserModule um ON u.Id = um.UserId
         JOIN training.Module m ON um.TrainingModuleId = m.Id
         JOIN training.TrainingTask tt ON um.TrainingModuleId = tt.ParentTrainingModuleId
         JOIN training.UserTask ut ON um.Id = ut.UserTrainingModuleId
) AS sourceTable PIVOT(MAX(lastCompleted) FOR courseName IN([Anti-Fraud],
                                                            [Approved Assessors and UNIQUS])) AS pivotTable;

当前输出:

userForeName userSurname Anti-Fraud Approved Assessors和UNIQUS Adam Conneely课程未开课或不完整课程未开课或不完整 Adam Conneely课程未开课或不完整课程未开课或不完整 Adam Conneely 2017-09-29 00:00:00.000课程未开始或不完整

预期产出:

userForeName userSurname Anti-Fraud Approved Assessors和UNIQUS Adam Conneely 2017-09-29 00:00:00.000课程未开始或不完整

0 个答案:

没有答案