当前位置: 代码迷 >> Sql Server >> 【求】sql语句 分不多 100
  详细解决方案

【求】sql语句 分不多 100

热度:285   发布时间:2016-04-24 09:26:43.0
【求】求一个sql语句 分不多 100 在线等


问题在图片上面    急求  在线等
------解决思路----------------------
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

  相关解决方案