SELECT
lms_app.paymentsadjincome_view.contractno,
lms_app.paymentsadjincome_view.currency_code,
lms_app.paymentsadjincome_view.duedate,
lms_app.paymentsadjincome_view.amount,
lms_app.paymentsadjincome_view.er_fp,
lms_app.paymentsadjincome_view.status,
lms_app.paymentsadjincome_view.recoupable_yn,
lms_app.paymentsadjincome_view.periodstartdate,
lms_app.paymentsadjincome_view.periodenddate,
lms_app.paymentsadjincome_view.paygroupno,
lms_app.paymentsadjincome_view.paygrptype,
lms_app.paymentsadjincome_view.paymentcategory::character varying,
lms_app.paymentsadjincome_view.currentarearespcode,
lms_app.paymentsadjincome_view.docsid,
lms_app.paymentsadjincome_view.payeesid,
lms_app.paymentsadjincome_view.paygroupsid,
lms_app.paymentsadjincome_view.paymentsid,
'N',
PAYMNTCHECKREQSTFULLSAP_VIEW_PAIV.paymentmethod,
lms_app.paymenttransdetails_view.sapglaccount,
'N'
FROM
lms_app.paymenttransdetails_view 
INNER JOIN lms_app.paymentsadjincome_view ON 
(lms_app.paymenttransdetails_view.contractno = lms_app.paymentsadjincome_view.contractno 
AND lms_app.paymenttransdetails_view.docsid = lms_app.paymentsadjincome_view.docsid 
AND lms_app.paymenttransdetails_view.paygroupsid = lms_app.paymentsadjincome_view.paygroupsid
AND
(lms_app.paymenttransdetails_view.paymentsid = lms_app.paymentsadjincome_view.paymentsid)
INNER JOIN lms_app.paymntcheckreqstfullsap_view PAYMNTCHECKREQSTFULLSAP_VIEW_PAIV ON (PAYMNTCHECKREQSTFULLSAP_VIEW_PAIV.paymentsid=lms_app.paymentsadjincome_view.paymentsid)

WHERE
( lms_app.paymentsadjincome_view.status NOT IN ('CANCELLED','START') )
AND
lms_app.paymentsadjincome_view.currentarearespcode IN ('1530','1531','1556','1624','1632','1637','1641','1714','1717','1834','1835','1838','1841','1634')
AND lms_app.paymentsadjincome_view.duedate between '2013-03-25' and '2013-05-22'
AND
lms_app.paymentsadjincome_view.paygrptype IN ('Earned Overriding Royalty','Earned Overriding Royalty - Calculate Only','Earned Royalty','Earned Royalty - Calculate Only','Earned Wheelage Royalty')
AND
lms_app.paymentsadjincome_view.amount <> 0
AND
lms_app.paymenttransdetails_view.sapglaccount IN ('0053010000','0053015000','0087220000')
