求一条FoxPro语句,查询出表中按某个字段分类的最大日期的记录!内详,高手入!
表table1(id,acctno,date),其中有记录:
1,0001,20120102
2,0001,20120103
3,0002,20120102
4,0002,20120104
...
要求:查询出表table1中每个acctno日期最大的那条记录。查询结果如:
2,0001,20120103
4,0002,20120104
...
[解决办法]
本帖最后由 apple_8180 于 2012-08-30 11:15:38 编辑
*-- vfp9.0 以下
Select id,acctno,Max(date) As date From table1 Group By acctno
*-- vfp9.0
Sys(3099,70)
Select id,acctno,Max(date) As date From table1 Group By acctno
Sys(3099,90)
Select max(id) As id,acctno,Max(date) As date From table1 Group By acctno
create table temp (id int,acctno c(10),date c(10))
insert into temp values (1,'0001','20120102')
insert into temp values (2,'0001','20120103')
insert into temp values (3,'0002','20120102')
insert into temp values (4,'0002','20120104')
Select a.id,b.acctno,b.date From temp a,(Select acctno,Max(date) as date From temp Group By acctno ) b Where a.acctno = b.acctno And a.date = b.date
*--- VFP9.0(汉化版) 代码
*--- 文件版本:9.0.0.7423
Select id,acctno,BIANH,MAX(date) DATE From temp GROUP BY ACCTNO
Create Table table1 (Id Int,acctno c(10),bianh c(10),Date c(10))
Insert Into table1 Values (1,'0001','1111','20120102')
Insert Into table1 Values (2,'0001','2222','20120103')&& 取此条
Insert Into table1 Values (3,'0002','9999','20120102')
Insert Into table1 Values (4,'0002','6666','20120104')&& 取此条
Browse&& 看一下原始记录
Select * From table1 A Where Not Exists (Select 1 From table1 B Where A.acctno=B.acctno And A.Date<B.Date)
*-- 或
*-- 下面这句可能需要 vfp9.0
Select a.Id,b.acctno,a.bianh,b.Date From table1 a,(Select acctno,Max(Date) As Date From table1 Group By acctno ) b Where a.acctno = b.acctno And a.Date = b.Date