首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何选择不同的行,但最大日期?

如何选择不同的行,但最大日期?
EN

Stack Overflow用户
提问于 2019-10-17 19:26:01
回答 1查看 30关注 0票数 0

假设我有一张表:

代码语言:javascript
运行
复制
Table A
Period Name| Type Name|    Date     | Quantity
-----------------------------
Yearly  | General | 2019-09-01  | 24
Yearly  | General | 2019-11-01  | 30
Yearly  | Casual  | 2019-09-01  | 6

我想让所有的行都按PeriodName,TypeName,但最大的日期分组。

我有group by max date,然后与原始表连接以获得结果:

代码语言:javascript
运行
复制
SELECT     dbo.tbl_WPT_LeaveType.TypeName, dbo.tbl_WPT_EmployeeLeavePolicy.FK_tbl_WPT_LeaveType_ID, dbo.tbl_WPT_LeavePeriod.PeriodName, 
                      dbo.tbl_WPT_EmployeeLeavePolicy.FK_tbl_WPT_LeavePeriod_ID, dbo.tbl_WPT_EmployeeLeavePolicy.Leave, dbo.tbl_WPT_EmployeeLeavePolicy.WithOutRequest, 
                      dbo.tbl_WPT_EmployeeLeavePolicy.ApplyFrom
FROM         dbo.tbl_WPT_EmployeeLeavePolicy INNER JOIN
                      dbo.tbl_WPT_LeaveType ON dbo.tbl_WPT_EmployeeLeavePolicy.FK_tbl_WPT_LeaveType_ID = dbo.tbl_WPT_LeaveType.ID INNER JOIN
                      dbo.tbl_WPT_LeavePeriod ON dbo.tbl_WPT_EmployeeLeavePolicy.FK_tbl_WPT_LeavePeriod_ID = dbo.tbl_WPT_LeavePeriod.ID INNER JOIN
   ---------same table to get max date                   
                          (SELECT     TOP (100) PERCENT MAX(tbl_WPT_EmployeeLeavePolicy_1.ApplyFrom) AS ApplyFrom, tbl_WPT_EmployeeLeavePolicy_1.FK_tbl_WPT_LeaveType_ID, 
                                                   tbl_WPT_EmployeeLeavePolicy_1.FK_tbl_WPT_LeavePeriod_ID
                            FROM          dbo.tbl_WPT_EmployeeLeavePolicy AS tbl_WPT_EmployeeLeavePolicy_1 INNER JOIN
                                                   dbo.tbl_WPT_LeaveType AS tbl_WPT_LeaveType_1 ON tbl_WPT_EmployeeLeavePolicy_1.FK_tbl_WPT_LeaveType_ID = tbl_WPT_LeaveType_1.ID INNER JOIN
                                                   dbo.tbl_WPT_LeavePeriod AS tbl_WPT_LeavePeriod_1 ON tbl_WPT_EmployeeLeavePolicy_1.FK_tbl_WPT_LeavePeriod_ID = tbl_WPT_LeavePeriod_1.ID
                            WHERE      (tbl_WPT_EmployeeLeavePolicy_1.FK_tbl_WPT_Employee_ID = @EmpID) AND (tbl_WPT_EmployeeLeavePolicy_1.FK_tbl_WPT_LeavePeriod_ID = 2) AND 
                                                   (tbl_WPT_EmployeeLeavePolicy_1.ApplyFrom <= @MonthFrom)
                            GROUP BY tbl_WPT_EmployeeLeavePolicy_1.FK_tbl_WPT_LeaveType_ID, tbl_WPT_EmployeeLeavePolicy_1.FK_tbl_WPT_LeavePeriod_ID) AS x ON x.FK_tbl_WPT_LeaveType_ID = dbo.tbl_WPT_EmployeeLeavePolicy.FK_tbl_WPT_LeaveType_ID AND x.FK_tbl_WPT_LeavePeriod_ID = dbo.tbl_WPT_EmployeeLeavePolicy.FK_tbl_WPT_LeavePeriod_ID AND x.ApplyFrom = dbo.tbl_WPT_EmployeeLeavePolicy.ApplyFrom


WHERE     (dbo.tbl_WPT_EmployeeLeavePolicy.FK_tbl_WPT_Employee_ID = @EmpID) AND (dbo.tbl_WPT_EmployeeLeavePolicy.FK_tbl_WPT_LeavePeriod_ID = 2) AND 
                      (dbo.tbl_WPT_EmployeeLeavePolicy.ApplyFrom <= @MonthFrom)
代码语言:javascript
运行
复制
Period Name| Type Name|    Date     | Quantity
-----------------------------------------------
Yearly  | General | 2019-11-01  | 30
Yearly  | Casual  | 2019-09-01  | 6
EN

回答 1

Stack Overflow用户

发布于 2019-10-17 21:20:28

我建议使用公用表表达式和row_number()函数来标识具有最大日期的记录。

例如:

代码语言:javascript
运行
复制
create table #test (
    [Period Name] varchar(20),
    [Type Name] varchar(20),
    [Date] date,
    Quantity int
)
insert #test values ('Yearly', 'General', '2019-09-01', 24)
                  , ('Yearly', 'General', '2019-11-01', 30)
                  , ('Yearly', 'Casual', '2019-09-01', 6)

;with c as (select [Period Name]
                 , [Type Name]
                 , [Date]
                 , Quantity
                 , row_number() over (partition by [Period Name], [Type Name] order by [date] desc) rn
              from #test
              )
select *
  from c
 where rn = 1

结果如下:

代码语言:javascript
运行
复制
Period Name Type Name   Date    Quantity    rn
Yearly  Casual  2019-09-01  6   1   
Yearly  General 2019-11-01  30  1   
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58431531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档