SELECT mcdt.customcontractid "Contract ID", mcdt.CONTRACTNAME AS "Contract Name", mcdt.customcontractid "Contract ID", contractcategory.Categoryname "Contract Type", LONGTODATE(mcdt.CREATEDDATE) AS "Created Time", ad.ORG_NAME AS "Account", rs.RESOURCENAME AS "Asset Name", product.COMPONENTNAME AS "Product", rs.SERIALNO AS "Org Serial Number" FROM MaintenanceContract mcdt LEFT JOIN Contract_Fields cadf ON mcdt.CONTRACTID=cadf.CONTRACTID LEFT JOIN VendorDefinition vdn ON mcdt.MAINTENANCEVENDOR=vdn.VENDORID LEFT JOIN SDOrganization aao ON vdn.VENDORID=aao.ORG_ID LEFT JOIN SDUser cby ON mcdt.CREATEDBY=cby.USERID LEFT JOIN AaaUser cbyaau ON cby.USERID=cbyaau.USER_ID LEFT JOIN ContractStatus cst ON mcdt.STATUSID=cst.STATUSID INNER JOIN ContractAccountmapping camp ON mcdt.contractid=camp.contractid INNER JOIN AccountDefinition ad ON camp.accountid=ad.org_id INNER JOIN ContractAccountMapping ON mcdt.CONTRACTID=ContractAccountMapping.CONTRACTID LEFT JOIN contractcategory ON mcdt.categoryid=contractcategory.categoryid
LEFT JOIN Contractdetails cd ON mcdt.contractid=cd.contractid LEFT JOIN Resources rs ON cd.resourceid=rs.resourceid
LEFT JOIN ComponentDefinition product ON rs.COMPONENTID=product.COMPONENTID LEFT JOIN ResourceLocation resLocation ON rs.RESOURCEID=resLocation.RESOURCEID
WHERE mcdt.CREATEDDATE >= CAST(EXTRACT(EPOCH FROM TIMESTAMP '2018-08-01 00:00:00') * 1000 AS BIGINT) AND mcdt.CREATEDDATE <= CAST(EXTRACT(EPOCH FROM TIMESTAMP '2020-08-30 00:00:00') * 1000 AS BIGINT) ORDER BY 1