条件成果基础上获得小组第一笔记录 [英] get first record in group by result base on condition
问题描述
这是我的数据库结构
create database testGroupfirst;
去
使用testGroupfirst;
go
创建表testTbl(
id int主键标识,名称nvarchar(50),年份int,度数int,位置nvarchar(50)
)
插入testTbl值('jack',2015,50,'giza')
插入testTbl值('jack',2016,500,' cai')
insert into testTbl values('jack',2017,660,'alex')
insert into testTbl values('jack',2018,666,'giza')
insert ('jack',2011,50,'alex')
插入testTbl值('rami',2015,5054,'giza')
插入testTbl值('rami', '''$'
插入testTbl值('rami',2017,66220,'giza')
插入testTbl值('rami',2018,6656,'alex')
插入testTbl值('rami',2011,540,'cai')
插入testTbl值('jack',2010,50,'cai')
select * from testTbl
这是迄今为止的结果
as如图2所示 - 我想获取第一个用户的数据和第一年的详细描述
- 光标可以做到这一点,但我不想使用它,我认为有很多方法可以以更好的方式处理情况,如交叉申请或cte。
我在这里找到一个相关的问题
获得前1行每个组
但这不适用于我的情况或我无法应用它,所以我在这里提出了这个新问题。
所以我需要从每个组(用户)获得第一行(前1),并希望获得第一条记录,并且必须包含用户在其中工作的第一年(按desc排序)。
<解决方案其中一个可能的解决方案是:
select name, (年份,学位,地点
从
(选择
名称,
年份,
学位,
地方,
row_number()按年份名称排序)
from testTbl
- 按名称,年份,学位,地点分组 - 不需要
)r
其中rn = 1;
更新:另一个解决方案将使用 CROSS APPLY
:
select t。*
from testTbl t
cross apply(从testTbl中选择top 1 id,其中name = t.name by year)r
其中t.id = r.id
this is my database structure create database testGroupfirst; go
use testGroupfirst;
go
create table testTbl (
id int primary key identity,name nvarchar(50) ,year int ,degree int , place nvarchar(50)
)
insert into testTbl values ('jack',2015,50,'giza')
insert into testTbl values ('jack',2016,500,'cai')
insert into testTbl values ('jack',2017,660,'alex')
insert into testTbl values ('jack',2018,666,'giza')
insert into testTbl values ('jack',2011,50,'alex')
insert into testTbl values ('rami',2015,5054,'giza')
insert into testTbl values ('rami',2016,1500,'cai')
insert into testTbl values ('rami',2017,66220,'giza')
insert into testTbl values ('rami',2018,6656,'alex')
insert into testTbl values ('rami',2011,540,'cai')
insert into testTbl values ('jack',2010,50,'cai')
select * from testTbl
this is the result till now
i created simple group by
select name , year ,degree ,place from testTbl group by name ,YEAR ,degree,place
as shown in pic 2 - I want to get the data for the first user and detailed description in their first year - cursor could make that but I didn't want to use it, and I think that there are many ways could handle the situation in the better way like cross apply or cte . I found a related question here Get top 1 row of each group but this doesn't work in my situation or I couldn't apply it so I made this new question here.
so i need to get the first row (top 1) from every group (user) and want to get the first record and must contains the first year that user works in it (order by desc)
One of the possible solutions is:
select name, year, degree, place
from
(select
name,
year,
degree,
place,
row_number() over (partition by name order by year) rn
from testTbl
--group by name ,YEAR ,degree,place -- not really needed
) r
where rn = 1;
UPDATE: Another solution (since both I and Tim posted the same) would be to use CROSS APPLY
:
select t.*
from testTbl t
cross apply (select top 1 id from testTbl where name = t.name order by year) r
where t.id = r.id
这篇关于条件成果基础上获得小组第一笔记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!