猿问

SQL 2005 查询的问题!

取每一个用户 最后一天的数据。表中 :用户ID(UserId),更新日期(UpdateDate),更新动作(Action)

表中数据:用户的最后更新日期  是不一定相同。这三列数据都需要查出来。

一直 没有想通怎么写。求高手指点!

梵蒂冈之花
浏览 505回答 8
8回答

白衣染霜花

select * from 表 left join (select UserId,max(UpdateDate) as UpdateDate from 表 group by userid) a on 表.userid=a.userid and 表.updatedate=a.updatedate

婷婷同学_

我有一个有方法 感觉有点笨你可以看一下 我用我自己表试了一下没有问题 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

繁星点点滴滴

方法可以实现。现在就是不知道 有没有更好点的方法。

慕姐8265434

@紫炁星:  如果有更好的说一声,我也学习一下

SMILET

有一个更笨的方法,先把更新日期按照降序排列,使用select语句选出top 1就行了!

慕桂英4014372

查询的是用户最后一天 (一组数据)的数据 并不是第一条数据。

慕沐林林

@紫炁星: 哈哈~谢谢提醒哦!我看题太粗心了~

梦里花落0921

max不就可以了。
随时随地看视频慕课网APP

相关分类

SQL Server
我要回答