有表A:
ID ,NAME ,source,BirthDay
1 ivan 100 1898-08-10
1 ivan 99 1899-08-10
2 ivan1 10 1998-08-10
2 ivan 1 11 1997-08-10
如果想找出相同id 的最大AGE值,如何实现?
Create table a (id integer ,name varchar(20),source integer,birthday datetime)
go
insert into (id,name,source,birthday)
select 1,'ivan',100,'1898-08-10'
union all select 1,'ivan',99,'1899-08-10'
union all select 2, 'ivan1', 10 , '1998-08-10'
union all select 2,'ivan1',11,'1997-08-10'
go
--实现上面的要求,可以用一句sql 实现
select * from a where exists(select 1 from a as b where b.id=a.id and a.source>b.source)
--方法二:
select a.name,max(a.source),(select b.birthday from a as b where a.name=b.name and b.source=max(a.source)) from a group by name
go
本文介绍了一种使用SQL查询来找出表中具有相同ID记录的最大AGE值的方法,并提供了两种不同的实现方式。
3万+

被折叠的 条评论
为什么被折叠?



