SQL-如何在特定列中选择具有最大值的数据行? [版本2]

时间:2019-02-13 09:05:50

标签: sql sql-server

尝试使用以下SQL创建具有2个不同视图的新视图:

SELECT a.[CourseID], a.[Course Code], a.[Course Title], a.[Result], a.[Employee No], 
   a.[Employee Name], a.[Section], max(b.ActStartDate) as [Last Training Date],
   b.ValidTo as [Validity Period],a.[Status], a.[Flag]
FROM [SQLDB3].[dbo].[Emp-OJT] as a 
INNER JOIN [SQLDB3].[dbo].[TrainingCourse_Record_Schedule] as b
   on a.[CourseID] = b.CourseID
GROUP BY a.[CourseID], a.[Course Code], a.[Course Title], a.[Result],a.[Employee No], a.[Employee Name], a.[Section], b.ValidTo,a.[Status], a.[Flag]

我的目标是获得一个表结果,该表结果为每个唯一的[课程代码]和[员工编号]组合提供单行数据,其[上次培训日期]列中包含该组合的最大值。

作为其当前状态,显示屏显示一些条目的多个数据的示例。请注意,我删除了一些不相关的列:

CourseID | Course Code | Course Title | Employee No | Employee Name | Last Training Date | Validity Period | Status |   Flag
------------------------------------------------------------------------------------------------------------------------------------------------------
6        | A001        | Admin        | 100002      | DUMM          |  2018-08-08        | NULL            | X      | Active
6        | A001        | Admin        | 100002      | DUMM          |  2019-02-07        | NULL            | X      | Active

我希望它仅显示[Last Training Date]2019-02-07的数据。对于内部连接语句,我什至尝试将a.[CourseID] = b.CourseID更改为其他来源,例如[Employee No]。但是到目前为止,它仍然不起作用。

解决此问题的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

使用申请

SELECT 
    a.[CourseID], a.[Course Code], a.[Course Title], a.[Result], a.[Employee No], 
    a.[Employee Name], a.[Section], b.ActStartDate AS [Last Training Date],
    b.ValidTo as [Validity Period],a.[Status], a.[Flag]
FROM
[SQLDB3].[dbo].[Emp-OJT] A
OUTER APPLY (
    SELECT TOP 1 ActStartDate, ValidTo 
    FROM [SQLDB3].[dbo].[TrainingCourse_Record_Schedule] 
    WHERE CourseID = A.CourseID
    ORDER BY ActStartDate DESC
) B

对于SQL Server 2000,请使用相关子查询

SELECT 
    a.[CourseID], a.[Course Code], a.[Course Title], a.[Result], a.[Employee No], 
    a.[Employee Name], a.[Section],
    a.[Status], a.[Flag], 
    (   SELECT TOP 1 ActStartDate 
        FROM [SQLDB3].[dbo].[TrainingCourse_Record_Schedule] 
        WHERE CourseID = A.CourseID
        ORDER BY ActStartDate DESC) AS [Last Training Date],
    (   SELECT TOP 1 ValidTo 
        FROM [SQLDB3].[dbo].[TrainingCourse_Record_Schedule] 
        WHERE CourseID = A.CourseID
        ORDER BY ActStartDate DESC) AS [Last Training Date],
FROM
[SQLDB3].[dbo].[Emp-OJT] A

答案 1 :(得分:0)

您可以尝试使用row_number()

select * from
(
SELECT a.[CourseID], a.[Course Code], a.[Course Title], a.[Result], a.[Employee No], 
   a.[Employee Name], a.[Section], max(b.ActStartDate) as [Last Training Date],
   b.ValidTo as [Validity Period],a.[Status], a.[Flag],row_number() over(partition by a.[CourseID],a.[Employee Name] order by b.ActStartDate desc) as rn
   FROM [SQLDB3].[dbo].[Emp-OJT] as a inner join [SQLDB3].[dbo].[TrainingCourse_Record_Schedule] as b
   on a.[CourseID] = b.CourseID
   group by a.[CourseID], a.[Course Code], a.[Course Title], a.[Result],a.[Employee No], a.[Employee Name], a.[Section], b.ValidTo,a.[Status], a.[Flag]
)A where rn=1