2014-09-19 28 views
1

我已經在SQL Server中下面的查詢顯示多個最大列:如何從最大ID

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。原因是我希望它只有最大的Leave ID具有相同的PayPeriod。我將如何格式化查詢以實現此目的?謝謝。

+0

如果你知道你想消滅你能不能只是添加一個附加條件和'LEAVEID NOT IN(112)許可ID' – MethodMan 2014-09-19 18:01:11

+0

你真應該看看正常化您的數據。您將EmployeeName作爲格式化值存儲在每一行中。您還要存儲LeaveCode的文本。這兩個應該是另一個表的外鍵。與ApprovalDepartment一樣。 – 2014-09-19 18:02:42

+0

DJ Kraze:我正在使用這個例子,所以我在這種情況下聲明瞭EmployeeName。實際上,對於ApprovalDepartment,IsApprove和IsFinalApprove,只有where子句將有100行。另一位員工可以在相同的付款期限內請求日期,但是LeaveID與這種情況類似。我只是不希望在這個例子中使用LeaveID 112,而不必像上面提到的那樣使用靜態條件。 – JonL 2014-09-19 18:08:35

回答

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 
+0

這似乎也是一個看法。 – JonL 2014-09-19 19:44:53

0

的解決方案是運行一個子查詢選擇每個PayPeriodEmployeeName,然後inner join針對最大LeaveID過濾掉其他的葉子。

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 
+0

你的意思是我有兩個內部連接嗎?你將如何格式化它? – JonL 2014-09-19 18:38:05

+0

@JonL我的意思是你應該像對待其他連接一樣對待它,並把代碼片段放在其他連接之後 – jpw 2014-09-19 18:42:36

+0

我已經在其他兩個內部連接之後並在末尾的where子句之前添加了內部連接塊,並且它仍然返回其中有LeaveID 112。 – JonL 2014-09-19 18:49:19