从每个员工的数据中获取前 1
Get Top 1 From Data For Every Employee
我有一个查询生成这样的数据
select c.Id, a.userId as Payroll, c.CurrentLocation, c.Longitude, c.Latitude, c.TextOtherLocation, c.CreationDate
from PeopleDB.dbo.masterpeople a
inner join PeopleDB.dbo.masterPeople b on a.manager = b.userid
inner join PeopleTracking c on a.userId = c.payroll
where a.Manager = '20090036'
ORDER BY Id DESC
Id Payroll CurrentLocation Longitude Latitude TextOtherLocation CreationDate
51 20180002 Field Work Location 107.5588565 -6.9077868 6/13/2020 19:56
50 20180002 Field Work Location 107.5588565 -6.9077868 6/14/2020 19:56
49 20190026 Office 107.5587918 -6.9077061 6/15/2020 19:56
48 20190026 Field Work Location 107.5588565 -6.9077868 6/16/2020 19:56
47 20190026 Office 107.5588565 -6.9077868 6/17/2020 19:56
我怎样才能得到任何工资单的任何前 1 数据降序
从上面的数据来看,我想要的是:
Id Payroll CurrentLocation Longitude Latitude TextOtherLocation CreationDate
51 20180002 Field Work Location 107.5588565 -6.9077868 6/13/2020 19:56
49 20190026 Office 107.5587918 -6.9077061 6/15/2020 19:56
感谢您的帮助。
使用row_number()
:
select t.*
from (select c.Id, a.userId as Payroll, c.CurrentLocation, c.Longitude, c.Latitude, c.TextOtherLocation, c.CreationDate,
row_number() over (partition by a.userid order by c.CreatedDate desc) as seqnum
from PeopleDB.dbo.masterpeople a join
PeopleDB.dbo.masterPeople b
on a.manager = b.userid join
PeopleTracking c
on a.userId = c.payroll
where a.Manager = '20090036'
) t
where seqnum = 1
ORDER BY Id DESC;
请注意,a
、b
和 c
确实 是 table 别名的错误选择。这些应该是 table 缩写,分别类似于 mp1
、mp2
和 pt
。
我通常使用 row_number()
窗口函数将序数分配给这样的行。
select Id,Payroll,CurrentLocation,Longitude,Latitude, TextOtherLocation, CreationDate
from (
select c.Id, a.userId as Payroll, c.CurrentLocation, c.Longitude, c.Latitude, c.TextOtherLocation, c.CreationDate, row_number() over (partition by a.userId order by c.Id desc) as PayrollRowNum
from PeopleDB.dbo.masterpeople a
inner join PeopleDB.dbo.masterPeople b on a.manager = b.userid
inner join PeopleTracking c on a.userId = c.payroll
where a.Manager = '20090036') as PayrollData where PayrollRowNum = 1
这会为每一行分配原始编号,并为每一行重置编号 a.userId
。然后根据每个 a.userId
"group".
中的降序 c.Id
仅检索 "first" 一个
我有一个查询生成这样的数据
select c.Id, a.userId as Payroll, c.CurrentLocation, c.Longitude, c.Latitude, c.TextOtherLocation, c.CreationDate
from PeopleDB.dbo.masterpeople a
inner join PeopleDB.dbo.masterPeople b on a.manager = b.userid
inner join PeopleTracking c on a.userId = c.payroll
where a.Manager = '20090036'
ORDER BY Id DESC
Id Payroll CurrentLocation Longitude Latitude TextOtherLocation CreationDate
51 20180002 Field Work Location 107.5588565 -6.9077868 6/13/2020 19:56
50 20180002 Field Work Location 107.5588565 -6.9077868 6/14/2020 19:56
49 20190026 Office 107.5587918 -6.9077061 6/15/2020 19:56
48 20190026 Field Work Location 107.5588565 -6.9077868 6/16/2020 19:56
47 20190026 Office 107.5588565 -6.9077868 6/17/2020 19:56
我怎样才能得到任何工资单的任何前 1 数据降序 从上面的数据来看,我想要的是:
Id Payroll CurrentLocation Longitude Latitude TextOtherLocation CreationDate
51 20180002 Field Work Location 107.5588565 -6.9077868 6/13/2020 19:56
49 20190026 Office 107.5587918 -6.9077061 6/15/2020 19:56
感谢您的帮助。
使用row_number()
:
select t.*
from (select c.Id, a.userId as Payroll, c.CurrentLocation, c.Longitude, c.Latitude, c.TextOtherLocation, c.CreationDate,
row_number() over (partition by a.userid order by c.CreatedDate desc) as seqnum
from PeopleDB.dbo.masterpeople a join
PeopleDB.dbo.masterPeople b
on a.manager = b.userid join
PeopleTracking c
on a.userId = c.payroll
where a.Manager = '20090036'
) t
where seqnum = 1
ORDER BY Id DESC;
请注意,a
、b
和 c
确实 是 table 别名的错误选择。这些应该是 table 缩写,分别类似于 mp1
、mp2
和 pt
。
我通常使用 row_number()
窗口函数将序数分配给这样的行。
select Id,Payroll,CurrentLocation,Longitude,Latitude, TextOtherLocation, CreationDate
from (
select c.Id, a.userId as Payroll, c.CurrentLocation, c.Longitude, c.Latitude, c.TextOtherLocation, c.CreationDate, row_number() over (partition by a.userId order by c.Id desc) as PayrollRowNum
from PeopleDB.dbo.masterpeople a
inner join PeopleDB.dbo.masterPeople b on a.manager = b.userid
inner join PeopleTracking c on a.userId = c.payroll
where a.Manager = '20090036') as PayrollData where PayrollRowNum = 1
这会为每一行分配原始编号,并为每一行重置编号 a.userId
。然后根据每个 a.userId
"group".
c.Id
仅检索 "first" 一个