select 6 as SOURCETYPE, WAGE.ITSID as SOURCEID, ITSDATE,
WAGELINE.ACCWAGEEXPENSE as ACCDEBIT, 2009 as SUBACCDEBITTYPE, EXPENSE.ITSID as SUBACCDEBITID, EXPENSE.ITSNAME as SUBACCDEBIT, sum(GROSSWAGE-TAXESEMPLOYEE) as DEBIT,
'NetWagesPay' as ACCCREDIT, 2007 as SUBACCCREDITTYPE, EMPLOYEECATEGORY.ITSID as SUBACCCREDITID, EMPLOYEECATEGORY.ITSNAME as SUBACCCREDIT, sum(GROSSWAGE-TAXESEMPLOYEE) as CREDIT
from WAGE 
join WAGELINE on WAGELINE.ITSOWNER = WAGE.ITSID 
join WAGETYPE on WAGETYPE.ITSID = WAGELINE.WAGETYPE 
join EXPENSE on EXPENSE.ITSID = WAGETYPE.EXPENSE 
join EMPLOYEE on EMPLOYEE.ITSID = WAGE.EMPLOYEE
join EMPLOYEECATEGORY on EMPLOYEECATEGORY.ITSID = EMPLOYEE.ITSCATEGORY 
where WAGE.REVERSEDID is null and WAGELINE.REVERSEDID is null :WHEREADD
group by SOURCETYPE, SOURCEID, ITSDATE, ACCDEBIT, SUBACCDEBITTYPE, SUBACCDEBITID, SUBACCDEBIT, ACCCREDIT, SUBACCCREDITTYPE, SUBACCCREDITID, SUBACCCREDIT
  
  union all

select 6 as SOURCETYPE, WAGE.ITSID   as SOURCEID, ITSDATE,
ACCTAXEXPENSE as ACCDEBIT, 2009 as SUBACCDEBITTYPE, EXPENSE.ITSID as SUBACCDEBITID, EXPENSE.ITSNAME as SUBACCDEBIT, sum(WAGETAXLINE.ITSTOTAL) as DEBIT,
'EmploymentTaxesPay' as ACCCREDIT, 2003 as SUBACCCREDITTYPE, TAX.ITSID as SUBACCCREDITID, TAX.ITSNAME as SUBACCCREDIT, sum(WAGETAXLINE.ITSTOTAL) as CREDIT
from WAGE 
join WAGETAXLINE on WAGE.ITSID = WAGETAXLINE.ITSOWNER 
join TAX on TAX.ITSID = WAGETAXLINE.TAX
join EXPENSE on EXPENSE.ITSID = TAX.EXPENSE 
where WAGETAXLINE.REVERSEDID is null and WAGE.REVERSEDID is null :WHEREADD
group by SOURCETYPE, SOURCEID, ITSDATE, ACCDEBIT, SUBACCDEBITTYPE, SUBACCDEBITID, SUBACCDEBIT, ACCCREDIT, SUBACCCREDITTYPE, SUBACCCREDITID, SUBACCCREDIT
