select zipcode_3d as zipcode from tbl_TER_ZipCodeLink zcl with (nolock) inner join tbl_UT_ZipCodes_3D zc with (nolock) on zc.zip3d_id = zcl.zip3d_id inner join tbl_TER_Territories ter with (nolock) on ter.territoryId = zcl.territoryId where zcl.territoryid in () and ter.fsl_tablecode = and ter.ownerId = select distinct dst.dstState from tbl_CDR_Distributors cdr with (nolock) inner join tbl_DST_Distributors dst with (nolock) on dst.dstId = cdr.fsl_dstId and dst.dstCompanyType = 'B' where cdr.ownerId = and cdr.fsl_tablecode = and cdr.cdr_territoryId in () and nullif( dst.dststate, '' ) is not null declare @ownerId int = ; declare @ownerType varchar(3) = ; with data as ( select dst.dstid, dst.dstName, dst.dstCompanyType, dst.dstParentCo, dst.dstSalesChannel, dst.dstAddr1, dst.dstAddr2, dst.dstCity, dst.dstState, dst.dstZip, dst.dstBuyingGrp, dst.dstcountryid, dst.dstcountryid countryid, case when dst.dstCompanyType = 'P' then 'Parent Company' when dst.dstCompanyType = 'B' then 'Branch Location' when dst.dstCompanyType = 'G' then 'Buying Group' when dst.dstCompanyType = 'O' then 'Other Direct Ship Operator' when dst.dstCompanyType = 'R' then 'Re-Distributor' when dst.dstCompanyType = 'S' then 'Special Account' when dst.dstCompanyType = 'C' then 'Regional Distribution Center' when dst.dstCompanyType = 'X' then 'Non-1FS Applicable' else '' end as displayCompanyType from tbl_DST_Distributors dst with (nolock) left outer join tbl_DW_CrmDistributors#_tableSuffix# cdr with (nolock) on dst.dstid = cdr.fsl_dstID and cdr.fsl_dstID is null where 1 = 1 and dst.dstState in () and left(dst.dstZip,3) in () ) #forJSON( 'data', attributes.dataStore, 'dstName' )#