>Table1 >Field Type Notes >------------------------------- >PK1 I PK >Emp I FK >Flag L >Amt Y >PK1_Emp_FLAG C(21) Value set to str(PK1)+str(Emp)+Flag in record validation() >Cust I FK >Type I FK > >Indexes >------- >PK1 Primary >Emp Regular >Flag Regular >PK1_Emp_Flag Candidate >Cust_Type Candidate [ str(Cust)+str(Type) ] >Deleted Regular > > >Table2 >------------------------------- >PK2 I PK >PK1 I FK (related to Table1.PK1) >Emp I FK (not necessary same value as in Table1) >Flag L >PK1_Emp_FLAG C(21) Value set to str(PK1)+str(Emp)+Flag in record validation() >Date D >Amt Y > >Indexes >------- >PK2 Primary >PK1 Regular >Emp Regular >Flag Regular >PK1_Emp_Flag Candidate >PK1_Emp_Flag_Date Candidate [ PK1_Emp_Flag+dtos(Date) ] >Deleted Regular >>
>Possible Scenario: >Table1 >------ >PK1 Emp Flag Amt >------------------------------ >1 1 .T. $25 >2 3 .F. $7 > >Table2 >------ >PK2 PK1 Emp Flag Date Amt >------------------------------------------------ >1 1 2 .F. {3/1/2} $10 >2 1 1 .T. {3/1/2) $15 >3 1 1 .T. {3/8/2) $5 >Note: there is no matching record in Table1 for Emp=2 > >I Need to Add these records to Table2: >4 1 2 .F. {3/15/2} -$10 >5 1 1 .T. {3/15/2} $5 >6 2 3 .F. {3/15/2) $7 >>
>The SQL's I'm currently using are: > >To Locate Info to create record 4: >Select Table2.PK1_Emp_Flag, Sum(Table2.Amt) as Paid, Table1.PK1_Emp_Flag ; > from Table2 left join Table1 ; > on Table2.PK1_Emp_Flag = Table1.PK1_Emp_Flag ; > group by 1 ; > having ; > isnull(Table1.PK1_Emp_Flag) ; > and Paid <> 0 > >To Locate Info to create records 5 & 6: >Select Table1.PK1_Emp_Flag, Table1.Amt, ; > sum(iif(isnull(Table2.Amt),0,Table2.Amt)) as Paid ; > from Table1 left join Table2 ; > on Table1.PK1_Emp_Flag = Table2.PK1_Emp_Flag ; > group by 1 ; > having ; > Table1.Amt <> Paid >