SELECT orig.TRAN_ID, orig.DIST, orig.ACT_COST, orig.POST_DATE, chng.DIST_CHG, chng.ACT_COST_CHG, chng.POST_DATE_CHG
FROM (
SELECT TABLE4.TRAN_ID AS TRAN_ID, Max(TABLE4.DIST) AS DIST, Last(TABLE4.ACT_COST) AS ACT_COST,
TABLE4.POST_DATE AS POST_DATE
FROM TABLE4
GROUP BY TABLE4.TRAN_ID, TABLE4.POST_DATE
HAVING (((TABLE4.TRAN_ID)="12345") And ((TABLE4.POST_DATE)=#2/28/2009#))
ORDER BY TABLE4.TRAN_ID, Max(TABLE4.DIST)) AS orig
INNER JOIN (
SELECT lid.TRAN_ID, lid.DIST AS DIST_CHG, tbl.ACT_COST AS ACT_COST_CHG, tbl.POST_DATE AS POST_DATE_CHG
FROM (
SELECT t1.TRAN_ID, MAX(t1.DIST) AS DIST
FROM Table4 AS t1
INNER JOIN Table4 AS t2 ON (t2.DIST=t1.DIST+1) AND (t2.TRAN_ID=t1.TRAN_ID)
WHERE t1.ACT_COST<>t2.ACT_COST
GROUP BY t1.TRAN_ID) lid
INNER JOIN Table4 tbl ON tbl.TRAN_ID = lid.TRAN_ID AND tbl.DIST = lid.DIST
) AS chng ON chng.TRAN_ID=orig.TRAN_ID ;
|