我正在访问的数据库有两个需要使用 DB2 SQL 查询的表,这里显示为 nametable 和 addresstable。该查询用于查找所有有一定余额的人。地址存储在单独的表中以跟踪地址更改。在addresstable中,最新的地址由一个序号(ADDRSEQUENCE)决定。AddressID 字段出现在两个表中,并将每个人与特定地址联系起来。最高的序列号是当前地址。我需要每个人的当前地址,而且只有那个。我知道我将不得不在某个地方使用 MAX 作为序列号,但我不知道如何在给定连接的情况下定位它。这是我当前的查询,当然会返回所有地址...
SELECT NAMETABLE.ACCTNUM AS ACCOUNTNUMBER,
NAMETABLE.NMELASTBUS AS LASTNAME,
NAMETABLE.NAME_FIRST AS FIRSTNAME,
NAMETABLE.BALDUE AS BALANCEDUE,
ADDRESSTABLE.STREETNAME AS ADDR,
ADDRESSTABLE.ADDRLINE2 AS
ADDRLINE2,ADDRESSTABLE.CITYPARISH AS CITY,
ADDRESSTABLE.ADDRSTATE AS STATE,
ADDRESSTABLE.ZIPCODE AS ZIP,
ADDRESSTABLE.ADDIDSEQNO AS ADDRSEQUENCE
FROM NAMETABLE JOIN ADDRESSTABLE ON NAMETABLE.ADDRESSID = ADDRESSTABLE.ADDRESSID
WHERE NAMETABLE.BALANCEDUE >= '50.00'