Query to show contract billing details with requests

Query to show contract billing details with requests

PGSQL:
‚Äč
select ad.org_name "Account", wo.workorderid "Request ID", wo.workorderid "Request ID", ac.contractname "Contract Name", sp.serviceplanname "Service Plan Name", sp.timeperiod "Bill Cycle", cast((sum(ct.TIMESPENT)/1000 * interval '1 second') as varchar) "Time Spent", LONGTODATE(wo.createdtime) "Creation Date", LONGTODATE(wo.completedtime) "Closing Date" from workorder wo LEFT JOIN workorderstates wos on wo.workorderid = wos.workorderid LEFT JOIN statusdefinition sd on sd.statusid = wos.statusid
left join accountsitemapping asm on asm.siteid = wo.siteid 
inner join accountcontract ac on ac.accountid = asm.accountid
left join serviceplan sp ON ac.serviceplanid=sp.serviceplanid
left join billcycle bc ON ac.contractid=bc.contractid 
Left join accountdefinition ad on asm.accountid = ad.org_id
LEFT JOIN WorkOrderToCharge wotoc ON wo.WORKORDERID=wotoc.WORKORDERID 
LEFT JOIN ChargesTable ct ON wotoc.CHARGEID=ct.CHARGEID 
where ac.accountid in ($Account) AND wo.createdtime>=<from_thismonth>and wo.createdtime<=<to_thismonth>
group by wo.workorderid, wo.createdtime, wo.completedtime, ad.org_name, sp.timeperiod, ac.startdate,ac.expirydate,sp.serviceplanname,sp.FIXEDMONTHLYUNITS,ac.contractno,ac.contractname ORDER BY 2 ASC