with bids as ( select b.bidId, case when b.bidStatus = 'TodoLines' then 'NL' when b.bidStatus = 'TodoProducts' then 'NP' when b.bidStatus = 'TodoQuote' then 'RD' when b.bidStatus = 'TodoDistrib' then 'ND' when b.bidStatus = 'WaitPricing' then 'WM' when b.bidStatus = 'WaitAward' then 'WA' when b.bidStatus = 'Archived' then 'ZZ' when b.bidStatus = 'Closed' then 'CL' else '' end as bidStatus, b.bidNumber, b.bidName, b.releaseDate, b.bidOpeningDateTime , a.bidAgencyId from tbl_BID_Bids b with (nolock) inner join tbl_BID_Agencies a with (nolock) on a.ownerId = b.ownerId and a.ownerType = b.ownerType and a.bidAgencyId = b.bidAgencyId innerleft outer join tbl_OPR_ClientOperators opr with (nolock) on opr.ownerId = a.ownerId and opr.fsltablecode = a.ownerType and opr.operatorId = a.partnerId and opr.territoryId in ( ) inner join tbl_BID_BidDistributors bdd with (nolock) on bdd.ownerId = b.ownerId and bdd.ownerType = b.ownerType and bdd.bidId = b.bidId inner join tbl_BID_Distributors bd with (nolock) on bd.ownerId = bdd.ownerId and bd.ownerType = bdd.ownerType and bd.bidDstId = bdd.bidDstId innerleft outer join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = bd.ownerId and cdr.fsl_tablecode = bd.ownerType and cdr.cdr_recordId = bd.cdr_recordId and cdr.cdr_territoryId in ( ) where b.ownerId = and b.ownerType = and b.deleted = 'N' and opr.operatorId = and cdr.cdr_recordId = ) #forJSON( 'bids', attributes.datastore, 'bidOpeningDateTime DESC' )#