分组查询,查出表1各个编号中,日期最大的一条记录,结果如表2

2025-03-10 08:26:01
推荐回答(3个)
回答1:

SELECT t1.*
FROM T1, (SELECT 编号, MAX(缴费日期) 缴费日期FROM T1 GROUP BY 编号) T2
WHERE T1.编号 = T2.编号
AND T1.缴费日期 = T2.缴费日期;

回答2:

select * from 表1 a where not exists(select * from 表1 b where a.编号=b.编号 and b.缴费日期>a.缴费日期)

回答3:

用Sql Server来做吧:

create table #aaa
(
id varchar(10),
缴费代码 varchar(10),
次数 int,
缴费日期 varchar(10)
)

insert into #aaa select '0001','0135',1,'2012-9-1'

union select '0001','0131',2,'2012-8-1'

select * from #aaa

*****结果:
0001 0135 1 2012-9-1
0001 0131 2 2012-8-1

执行代码:
select id,缴费代码,次数,缴费日期 from #aaa

where 缴费日期 in (select max(缴费日期) from #aaa group by id)

*****结果: 0001 0135 1 2012-9-1