Thank you both of you… in the end… I just managed to get it done… IDK if it is the best way to do it or if it is the ortodox way but it seems to be working and throwing the desired results…
EmpExpense table needs to link to Task table on EmpID = Key1 and and ForeignSysRowID = RelatedToSysRowID.
Also, you will need to filter the Task table: RelatedToFile = ‘EmpExpense’ and Approved = 1 and Complete = 1 and TaskID =‘suprappr’
with that said… this is my query…
select EE.EmpID
, EB.Name
, EE.Company
, EE.ExpenseDate
, (Select Name from EmpBasic where EmpID = EB.SupervisorID and EmpStatus = 'A') as 'Approver'--EB.SupervisorID
, t.ChangeDcdUserID as ApprovedBy
, EE.ExpenseStatus
, EE.ApprovedDate
, EE.Character01
, ee.DocClaimAmt as 'Expense Amount'
, (select Description from erp.PurMisc where company = ee.company and MiscCode = ee.MiscCode) as 'Expense Type'
, EE.InvoiceNum
, APIH.InvoiceDate
, EE.Invoiced
, APIH.ApplyDate
, EE.Character02 as 'Date'
, APT.CheckNum
, APT.TranDate
, APIH.InvoiceAmt
, EE.ProjectID
--, ee.SysRowID
from empexpense EE
Inner JOIN EmpBasic EB On EE.EmpID = EB.EmpID and EE.Company = EB.Company
Inner JOIN APInvHed APIH On EE.EmpID = APIH.EmpID and EE.Company = APIH.Company and EE.InvoiceNum = APIH.InvoiceNum
INNER JOIN erp.APTran APT ON APIH.InvoiceNum = APT.InvoiceNum and APIH.Company = APT.Company
left outer join erp.Task T on ee.EmpID = t.Key1 and ee.Company = t.Company and ee.ApprovedDate = t.CompleteDate
where EE.ExpenseDate >= @StartDate
and EE.ExpenseDate <= @EndDate
--and ee.MiscCode <> 'PD'
and EE.Character01 <> 'TSheets'
--and eb.SupervisorID = t.SalesRepCode
and ee.ForeignSysRowID = t.RelatedToSysRowID
and EB.EmpStatus = 'A'
and t.RelatedToFile = 'EmpExpense' and t.Approved = 1 and t.Complete = 1 and t.TaskID ='suprappr'
Group By EE.EmpID, EB.Name, EE.Company, EB.SupervisorID, EE.ExpenseDate, EE.ExpenseStatus, EE.ApprovedDate, EE.Character01, EE.InvoiceNum, EE.Invoiced, APIH.InvoiceDate
, APIH.ApplyDate
, EE.Character02
, APT.CheckNum
, APT.TranDate
, APIH.InvoiceAmt
, EE.ProjectID
, T.ChangeDcdUserID
, ee.DocClaimAmt
, ee.MiscCode