select ac.contractno "Contract Number", ac.contractname "Contract Name", sp.serviceplanname "Service Plan Name",sp.plantype "Service Plan Type", sp.timeperiod "Bill Cycle",fixedmonthlycharges "Fixed Base Charge",sp.fixedmonthlyunits "Allowance", sp.fixedhourcost "Fixed Cost Per Hour",sp.operationalhourcost "OP Hrs Cost",
sp.nonoperationalhourcost "Non OP Hrs Cost", sp.weekendhourcost "Weekend Cost",sp.holidayhourcost "Holiday Cost", longtodate(ac.startdate) "Contract Start Date", longtodate(ac.expirydate) "Contract Expiry Date", ac.isactivecontract "Is Active Contract", ad.org_name "Account" 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
where ac.accountid in ($Account)
group by ad.org_name,ac.startdate,ac.expirydate,sp.serviceplanname,sp.FIXEDMONTHLYUNITS,ac.contractno,ac.contractname,sp.plantype,ac.isactivecontract,sp.timeperiod,sp.fixedmonthlycharges,sp.fixedhourcost,sp.operationalhourcost,sp.nonoperationalhourcost,sp.weekendhourcost,sp.holidayhourcost ORDER BY 8 ASC
With billing mode as prepaid and ONLY active contracts:
select ad.org_name "Account", ac.contractname "Contract Name", ac.isactivecontract "Is Active Contract", sp.serviceplanname "Service Plan Name",sp.plantype "Service Plan Type",sp.fixedmonthlyunits "Total Allowance", sum(ct.timespent/3600000) "Hours Used" , (sp.fixedmonthlyunits)+COALESCE(bh.closingbalance/1000/3600,0)-(sum(ct.timespent)/3600000) "Hours Remaining" 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 billhistory bh on ac.contractid=bh.contractid
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 wotc on wotc.workorderid=wo.workorderid
inner join chargestable ct on ct.chargeid=wotc.chargeid
left join NOBILLREQUESTCHARGES nbrc on nbrc.requestchargeid=ct.chargeid
where ac.accountid in ($Account) AND ac.isactivecontract='true' and sp.billingmode=1
group by ad.org_name,sp.serviceplanname,sp.FIXEDMONTHLYUNITS,ac.contractname,sp.plantype,ac.isactivecontract, bh.closingbalance ORDER BY 1 ASC
NOTE: ($Account) is incldued in the query. For account specific data, please select the Account at the top from the down