问题在图片上面 急求 在线等
------解决思路----------------------
with a(id,userid,schoolname) as
(
select 1,1,'社会大学' union all
select 2,1,'北京大学' union all
select 3,2,'社会大学' union all
select 4,2,'北京大学'
)
select c.id,c.userid,c.schoolname from
(select *,ROW_NUMBER()over(partition by userid order by getdate()) as b
from a) c where c.b=1
/**
------------------------
id userid schoolname
2 1 北京大学
4 2 北京大学
------------------------
**/
------解决思路----------------------
create table UserSchool
(
id int identity(1,1) not null primary key,
userId int,
SchoolName varchar(50)
)
insert into UserSchool values(1,'社会大学')
insert into UserSchool values(1,'北京大学')
insert into UserSchool values(2,'社会大学')
insert into UserSchool values(2,'北京大学')
select a.id,userId,SchoolName from
(select MAX(id)as id from UserSchool
group by userId) as a,UserSchool
where a.id=UserSchool.id