如何从最大ID显示多个最大列

时间:2014-09-19 17:56:42

标签: c# sql asp.net sql-server

我在SQL Server中有以下查询:

select 
    Max(STO.LeaveID) as LeaveID, 
    LR.EmployeeName, 
    STO.DateOff, 
    STO.TimeBegin, 
    STO.TimeEnd, 
    STO.PayPeriodEnd, 
    STO.TodayHoursOff, 
    STO.LeaveCode 
from 
    dbo.tblSeperateTimeOff STO
inner join 
    dbo.tblLeaveRequest LR on STO.LeaveID=LR.ID
inner join 
    dbo.tblLeaveApproval LA on STO.LeaveID = LA.LeaveID
where 
    LA.ApprovalDepartment like'%Finance%' 
    and EmployeeName like '%polland%' 
    and LA.IsApprove=1 
    and LA.IsFinalApprove=1 
group by 
    LR.EmployeeName, 
    STO.DateOff, 
    STO.TimeBegin, 
    STO.TimeEnd, 
    STO.PayPeriodEnd, 
    STO.TodayHoursOff, 
    STO.LeaveCode 
order by 
    EmployeeName

结果显示:

LeaveID EmployeeName    DateOff     TimeBegin   TimeEnd     PayPeriod   Hours   LeaveCode
88      Polland, Sean   2014-09-08  08:30AM     11:00AM     2014-09-13  2.5     P (Personal Leave Scheduled*)
112     Polland, Sean   2014-09-24                          2014-09-27  8       P (Personal Leave Scheduled*)
121     Polland, Sean   2014-09-25                          2014-09-27  8       P (Personal Leave Scheduled*)
121     Polland, Sean   2014-09-26                          2014-09-27  8       P (Personal Leave Scheduled*)

我想摆脱带有LeaveID 112的行并保留88和两个121的LeaveID。这样做的原因是我希望它仅从最大离开ID中获得相同的PayPeriod。如何格式化查询以实现此目的?感谢。

3 个答案:

答案 0 :(得分:1)

您可以使用相关子查询来过滤每个ID的最大值。尝试将此添加到您的内部联接:

inner join 
  (
    select max(LeaveID) maxleaveid , PayPeriodEnd
    from tblSeperateTimeOff
    group by PayPeriodEnd
  ) m on sto.leaveid = m.maxleaveid and sto.PayPeriodEnd = m.PayPeriodEnd

我认为这就是你所需要的:

select 
    Max(STO.LeaveID) as LeaveID, 
    LR.EmployeeName, 
    STO.DateOff, 
    STO.TimeBegin, 
    STO.TimeEnd, 
    STO.PayPeriodEnd, 
    STO.TodayHoursOff, 
    STO.LeaveCode 
from dbo.tblSeperateTimeOff STO
inner join dbo.tblLeaveRequest LR on STO.LeaveID=LR.ID
inner join dbo.tblLeaveApproval LA on STO.LeaveID = LA.LeaveID
inner join 
  (
    select max(LeaveID) maxleaveid , PayPeriodEnd
    from tblSeperateTimeOff
    group by PayPeriodEnd
  ) m on sto.leaveid = m.maxleaveid and sto.PayPeriodEnd = m.PayPeriodEnd
where 
    LA.ApprovalDepartment like'%Finance%' 
    and EmployeeName like '%polland%' 
    and LA.IsApprove=1 
    and LA.IsFinalApprove=1 
group by 
    LR.EmployeeName, 
    STO.DateOff, 
    STO.TimeBegin, 
    STO.TimeEnd, 
    STO.PayPeriodEnd, 
    STO.TodayHoursOff, 
    STO.LeaveCode 
order by EmployeeName

答案 1 :(得分:1)

这是你可以做到这一点的一种方式。

create table #Something
(
    LeaveID int
    , EmployeeName varchar(25)
    , DateOff date
    , TimeBegin time
    , TimeEnd time
    , PayPeriod date
    , Hours numeric(9,2)
    , LeaveCode varchar(50)
)

insert #Something
select 88, 'Polland, Sean', '2014-09-08', '08:30AM', '11:00AM', '2014-09-13', 2.5, 'P (Personal Leave Scheduled*)' union all
select 112, 'Polland, Sean', '2014-09-24', null, null, '2014-09-27', 8, 'P (Personal Leave Scheduled*)' union all
select 121, 'Polland, Sean', '2014-09-25', null, null, '2014-09-27', 8, 'P (Personal Leave Scheduled*)' union all
select 121, 'Polland, Sean', '2014-09-26', null, null, '2014-09-27', 8, 'P (Personal Leave Scheduled*)';

with SortedResults as
(
    select *
     , DENSE_RANK() over(partition by PayPeriod order by LeaveID desc) as GroupDepth
    from #Something
)

select *
from SortedResults
where GroupDepth = 1

drop table #Something

答案 2 :(得分:0)

解决方案是运行子查询,为每个LeaveIDPayPeriod选择最大EmployeeName,然后针对该inner join选择过滤掉其他离开的{{1}}。