求一查询语句,万分感谢!!!

zhongzuo1981 2005-08-19 05:04:40
table:
id perNum date
1 1234 2005-04-10
2 4568 2005-04-23
3 1234 2005-05-01
4 6987 2005-05-05
5 4568 2005-05-16
6 1234 2005-05-30
7 7894 2005-06-12

得出不重复的perNum的最新的那行记录,没有排序要求。
1234 2005-05-30
4568 2005-05-16
6987 2005-05-05
7894 2005-06-12
...全文
116 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
天地客人 2005-08-20
  • 打赏
  • 举报
回复
select perNum,max(date) as date
from table
group by perNum
filebat 2005-08-20
  • 打赏
  • 举报
回复
--和 vivianfdlpw一样, 也假定id为主键
select *
from 表名 as tt
where id=(select top 1 id
from 表名
where perNum=tt.perNum
order by [date] desc)
vivianfdlpw 2005-08-19
  • 打赏
  • 举报
回复
zlp321002(想在北京找份工作!) 的没有考虑日期并列的情况
zlp321002 2005-08-19
  • 打赏
  • 举报
回复
--测试环境
declare @t table(id int identity(1,1),perNum varchar(5), date datetime)
insert into @t select '1234','2005-04-10'
union all select '4568','2005-04-23'
union all select '1234','2005-05-01'
union all select '6987','2005-05-05'
union all select '4568','2005-05-16'
union all select '1234','2005-05-30'
union all select '7894','2005-06-12'
--查询语句
select * from @t a
where not exists (select 1 from @t where perNum=a.perNum and date>a.date)
order by perNum
--结果
id perNum date
----------- ------ ------------------------------------------------------
6 1234 2005-05-30 00:00:00.000
5 4568 2005-05-16 00:00:00.000
4 6987 2005-05-05 00:00:00.000
7 7894 2005-06-12 00:00:00.000

(所影响的行数为 4 行)


小李木耳 2005-08-19
  • 打赏
  • 举报
回复
不好意思,我看到好多‘星’就急了:
改---->
select perNum date from 表
union
select perNum date from 表


小李木耳 2005-08-19
  • 打赏
  • 举报
回复
select * from 表
union
select * from 表
vivianfdlpw 2005-08-19
  • 打赏
  • 举报
回复
如果id唯一:
select * from 表 t
where not exists(select 1 from 表 where perNum=t.perNum and id>t.id)
flyorgao 2005-08-19
  • 打赏
  • 举报
回复
select perNum, max(date ) from table group by perNum
子陌红尘 2005-08-19
  • 打赏
  • 举报
回复
select perNum,[date]=max([date]) from [table] group by perNum

or

select
a.perNum,a.[date]
from
[table] a
where
a.id = (select top 1 id from [table] where perNum=a.perNum order by [date] desc)
WangZWang 2005-08-19
  • 打赏
  • 举报
回复
select perNum,max(date) as date
from table
group by perNum

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧