* Table2 7,$60,^2004/10/29 && 7's first row 7,$60,^2004/10/29 && 7's second rowYou can use RECNO() like primary key,
create cursor Table1 ( Id I,ContrSum Y,ContrDate D) insert into Table1 values(1, $100, {^2004/10/10}) insert into Table1 values(2, $200, {^2004/10/11}) insert into Table1 values(3, $300, {^2004/10/13}) insert into Table1 values(4, $300, {^2004/10/14}) Create cursor Table2 ( idTable2 I AUTOINCR ,Id I, PayedSum Y, PayedDate D ) insert into Table2 (id,PayedSum,PayedDate) Values(1, $50, {^2004/10/10}) insert into Table2 (id,PayedSum,PayedDate) Values(1, $20, {^2004/10/10}) insert into Table2 (id,PayedSum,PayedDate) Values(1, $10, {^2004/10/11}) insert into Table2 (id,PayedSum,PayedDate) Values(1, $20, {^2004/10/12}) insert into Table2 (id,PayedSum,PayedDate) Values(2, $100, {^2004/10/11}) insert into Table2 (id,PayedSum,PayedDate) Values(2, $100, {^2004/10/13}) insert into Table2 (id,PayedSum,PayedDate) Values(3, $200, {^2004/10/14}) SELECT T1.id ; , IIF(MIN(T2.idTable2)>MIN(T3.idTable2),NULL,MIN(T1.ContrSum)) ContrSum; , IIF(MIN(T2.idTable2)>MIN(T3.idTable2),NULL,MIN(T1.ContrDate)) ContrDate; , MIN(T2.PayedSum) PayedSum; , MIN(T2.PayedDate) PayedDate; INTO CURSOR OK_Only_If_Input_id_And_PayedDate_It_Is_Unique; FROM Table1 T1 LEFT JOIN Table2 T2 ON T2.id=T1.id; LEFT JOIN Table2 T3 ON T3.id=T1.id; GROUP BY T1.id,T2.idTable2 BROWSE LAST * ATTENTION: previous it is ok only if, * : order of idTable2 and order by PayedDate put Table2 into the same order! * add a out of order payment insert into Table2 (id,PayedSum,PayedDate) Values(3, $10, {^2004/10/13}) SELECT T1.id ; , IIF(T2.idTable2>MIN(T3.idTable2),NULL,MIN(T1.ContrSum)) ContrSum; , IIF(T2.idTable2>MIN(T3.idTable2),NULL,MIN(T1.ContrDate)) ContrDate; , MIN(T2.PayedSum) PayedSum; , MIN(T2.PayedDate) PayedDate; INTO CURSOR FAIL_Only_If_Input_PayedDate_It_Is_Ordered; FROM Table1 T1 LEFT JOIN Table2 T2 ON T2.id=T1.id; LEFT JOIN Table2 T3 ON T3.id=T1.id; GROUP BY T1.id,T2.idTable2 BROWSE LAST * PayedDate it is not in order ! * you cannot resolve this , because if you reorder the table, you reoder the table1 fields SELECT MIN(T1.id) ; , IIF(T2.idTable2>MIN(T3.idTable2),NULL,MIN(T1.ContrSum)) ContrSum; , IIF(T2.idTable2>MIN(T3.idTable2),NULL,MIN(T1.ContrDate)) ContrDate; , MIN(T2.PayedSum) PayedSum; , MIN(T2.PayedDate) PayedDate; INTO CURSOR FAIL_Reorder_PayedDate_Reorder_NULLS; FROM Table1 T1 LEFT JOIN Table2 T2 ON T2.id=T1.id; LEFT JOIN Table2 T3 ON T3.id=T1.id; GROUP BY T2.idTable2; ORDER BY 1,5 BROWSE LAST * Solution: build a PayedDate ordered Unique key on the fly * ( Attention: VFP untyped Nulls fire a error at SELECT build plan time ) SELECT T1.id ; ,IIF(DTOS(T2.PayedDate)+BINTOC(T2.idTable2)>MIN(DTOS(T3.PayedDate)+BINTOC(T3.idTable2)); ,$0,MIN(T1.ContrSum)) ContrSum; ,IIF(DTOS(T2.PayedDate)+BINTOC(T2.idTable2)>MIN(DTOS(T3.PayedDate)+BINTOC(T3.idTable2)); ,{},MIN(T1.ContrDate)) ContrDate; , MIN(T2.PayedSum) PayedSum; , T2.PayedDate ; INTO CURSOR OK_Build_A_Ordered_PayedDate_Unique_Value_with_Empty; FROM Table1 T1 LEFT JOIN Table2 T2 ON T2.id=T1.id; LEFT JOIN Table2 T3 ON T3.id=T1.id; GROUP BY T1.id,T2.PayedDate,T2.idTable2 BROWSE LAST * if you want NULL ( the correct choice ) you can use a typed variable yNull=$0 dNull={} STORE NULL TO yNull,dNull SELECT T1.id ; ,IIF(DTOS(T2.PayedDate)+BINTOC(T2.idTable2)>MIN(DTOS(T3.PayedDate)+BINTOC(T3.idTable2)); ,m.yNull,MIN(T1.ContrSum)) ContrSum; ,IIF(DTOS(T2.PayedDate)+BINTOC(T2.idTable2)>MIN(DTOS(T3.PayedDate)+BINTOC(T3.idTable2)); ,m.dNull,MIN(T1.ContrDate)) ContrDate; , MIN(T2.PayedSum) PayedSum; , T2.PayedDate ; INTO CURSOR OK_Build_A_Ordered_PayedDate_Unique_Value_with_Null; FROM Table1 T1 LEFT JOIN Table2 T2 ON T2.id=T1.id; LEFT JOIN Table2 T3 ON T3.id=T1.id; GROUP BY T1.id,T2.PayedDate,T2.idTable2 BROWSE LASTFabio