取每一个用户 最后一天的数据。表中 :用户ID(UserId),更新日期(UpdateDate),更新动作(Action)
表中数据:用户的最后更新日期 是不一定相同。这三列数据都需要查出来。
一直 没有想通怎么写。求高手指点!
8 回答
白衣染霜花
TA贡献1796条经验 获得超10个赞
select * from 表 left join (select UserId,max(UpdateDate) as UpdateDate from 表 group by userid) a on 表.userid=a.userid and 表.updatedate=a.updatedate
婷婷同学_
TA贡献1844条经验 获得超8个赞
我有一个有方法
感觉有点笨你可以看一下
我用我自己表试了一下没有问题
declare @id int --建一个临时表 if object_id('Tempdb..#temp') is not null begin drop table #temp end else begin create table #temp ( [用户ID] int , [更新日期] varchar(100), [更新动作] varchar(200) ) end declare cursor1 cursor for --用游标 select distinct UserId from [你的表] open cursor1 fetch next from cursor1 into @id while @@fetch_status=0 begin insert into #temp select top 1 UserId,UpdateDate,Action from [你的表] where UserId = @id order by UpdateDate desc fetch next from cursor1 into @id end close cursor1 deallocate cursor1 select * from #temp drop table #temp
- 8 回答
- 0 关注
- 504 浏览
添加回答
举报
0/150
提交
取消