---
title: "多表联结查询怎样得到重复f01Id数据的最后一条记录"
author: "javaOpen"
date: "2007-11-23T11:30:33Z"
tags: []
slug: "f01id"
draft: false
lastmod: "2007-11-23T11:30:33Z"
id: 143001
url: "https://javaopen.iteye.com/blog/143001"
---

select  a.f01Id,a.costCenter,a.acctPeriod,a.amt,b.status 
from db.fr1 a,db.f01 b ,db.fb2 c,db.fb1 d
where a.f01Id=b.f01Id 
and b.fb2Id=c.fb2Id 
and c.fb1Id=d.fb1Id 
and a.compId= 'A1001'
and  a.acctPeriod <='2007-11' 
and b.status in ('A','B','C')  
order by d.SortNo,c.AssetNo,b.asSer 

其中查询出来的数据如
1001 W1009 2001-06 2000 A
1001 W1006 2003-10 5000 A
1001 W1009 2005-11 9000 A

1002 W1005 2001-06 2000 C
1002 W1006 2004-05 5000 C
1002 W1008 2006-11 8000 C

1005 W1009 2002-06 2000 B
1005 W1011 2007-10 5000 B
1005 W1088 2007-11 9600 B
在DB2中,我想要得到如下的数据怎么办?(不用程序处理,因为数据量太大(15 万条记录)) 就是我想取得每一个a.f01Id号的的最后一条记录,在数据库中直接处理.我用distinct了,但是联表查询不行,怎么办? 请教一下各位!谢谢,谢谢!
1001 W1009 2005-11 9000 A
1002 W1008 2006-11 8000 C
1005 W1088 2007-11 9600 B




<span style="color:blue"></span>