select customer_number, coalesce( customer_name, '' ) customer_name, customer_address, coalesce( customer_address2, '') customer_address2, customer_city, customer_state, customer_zipcode, customer_territory_id, sum( coalesce( case_qty, 0.0 )) case_qty, sum( coalesce( lb_qty, 0.0 ) ) lb_qty, sum( coalesce( extended_price, 0.0 )) extended_price from tbl_IMPORT_InvoiceHistory with (nolock) where ownerId = and ownerType = and eai_batchId = group by customer_number, customer_name, customer_address,customer_address2, customer_city, customer_state, customer_zipcode,customer_territory_id having sum( coalesce( case_qty, 0.0 )) <> 0 or sum( coalesce( lb_qty, 0.0 ) ) <> 0 or sum( coalesce( extended_price, 0.0 )) <> 0 select cdr.cdr_recordId, coalesce( cdr.bgtCatId, coalesce( pcdr.bgtCatId, 0 )) bgtCatId from tbl_CDR_DstCodes cn with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.cdr_recordId = cn.cdr_recordId and cdr.ownerId = cn.ownerId and cdr.fsl_tablecode = cn.ownerType left outer join tbl_DST_Distributors dst with (nolock) on dst.dstId = cdr.fsl_dstId left outer join tbl_CDR_Distributors pcdr with (nolock) on pcdr.fsl_dstId = dst.dstParentCo and pcdr.ownerId = cdr.ownerId and pcdr.fsl_tablecode = cdr.fsl_tablecode where cn.ownerId = and cn.ownerType = and cn.cdr_dstcode = select ter.territoryId from tbl_TER_Territories ter with (nolock) inner join tbl_TER_TerritoryLevels lvl with (nolock) on lvl.levelId = ter.levelId and lvl.ownerId = ter.ownerId and lvl.fsl_tablecode = ter.fsl_tablecode where ter.ownerId = and ter.fsl_tablecode = and ter.internalSalesID = select top 1 territoryId, name, levelId from tbl_TER_Territories with (nolock) where levelId = ( select top 1 levelId from tbl_TER_TerritoryLevels with (nolock) where ownerId = and fsl_tablecode = and rank = 1 order by levelId ) and ownerId = and fsl_tablecode = order by territoryId SELECT levelId FROM tbl_TER_TerritoryLevels WHERE OWNERID = and fsl_TableCode = and parentLevelID = insert into tbl_TER_Territories (OwnerID,FSL_TableCode, Name, LevelID, ParentTerritoryID, InternalSalesID,managementRank, oprEnabled ,cdrEnabled, sName, shareWithFoodMark,countryId ) values (, , , , , , 'A','Y','Y', , 'N',27 ) select ter.territoryId from tbl_TER_Territories ter with (nolock) inner join tbl_TER_TerritoryLevels lvl with (nolock) on lvl.levelId = ter.levelId and lvl.ownerId = ter.ownerId and lvl.fsl_tablecode = ter.fsl_tablecode and lvl.BrokerLevel = 1 and lvl.zipCodeMapping = 'STANDARD' inner join tbl_TER_ZipCodeLink zcl with (nolock) on zcl.territoryId = ter.territoryId inner join tbl_UT_ZipCodes_3d z3 with (nolock) on z3.zip3d_id = zcl.zip3d_id where ter.ownerId = and ter.fsl_tablecode = and z3.zipcode_3d = select top 1 territoryId, name from tbl_TER_Territories with (nolock) where levelId = ( select top 1 levelId from tbl_TER_TerritoryLevels with (nolock) where ownerId = and fsl_tablecode = and rank = 1 order by levelId ) and ownerId = and fsl_tablecode = order by territoryId insert into tbl_CDR_Distributors ( ownerId, fsl_tablecode, cdr_dstCompanyType, cdr_dstCode, cdr_dstName, recordSource, cdr_territoryId, crmActive, lastUpdated, cdr_dstAddress1,cdr_dstAddress2, cdr_dstcity, cdr_dststate, cdr_dstzip ) values( , , 'B', , , , , 'N', , , , , , ) insert into tbl_CDR_DstCodes ( ownerId, ownerType, cdr_recordId, cdr_dstCode, cdr_dstCode_type ) values( , , , , 'M' ) update tbl_IMPORT_InvoiceHistory set fseCDR_recordIdStatus = 'A' where ownerId = and ownerType = and eai_batchId = and customer_number = and coalesce( customer_name, '' ) = select cdr.cdr_recordId, coalesce( cdr.bgtCatId, coalesce( pcdr.bgtCatId, 0 )) bgtCatId from tbl_CDR_DstCodes cn with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.cdr_recordId = cn.cdr_recordId and cdr.ownerId = cn.ownerId and cdr.fsl_tablecode = cn.ownerType left outer join tbl_DST_Distributors dst with (nolock) on dst.dstId = cdr.fsl_dstId left outer join tbl_CDR_Distributors pcdr with (nolock) on pcdr.fsl_dstId = dst.dstParentCo and pcdr.ownerId = cdr.ownerId and pcdr.fsl_tablecode = cdr.fsl_tablecode where cdr.ownerId = and cdr.fsl_tablecode = and cdr.cdr_recordId = select top 1 cdr_RecordId, fsl_dstId from tbl_CDR_distributors where cdr_recordId in ( select cdr_RecordId from tbl_CDR_DstCodes where cdr_dstCode = and ownerId = and ownerType = ) and cdr_dstcompanyType in ('B', 'O') order by cdr_dstcompanyType, cdr_RecordId select distinct redist_customer_number from tbl_IMPORT_InvoiceHistory with (nolock) where ownerId = and ownerType = and eai_batchId = and customer_number = and reverseInvoice= select distinct cdr.cdr_recordId, cdr.cdr_dstName, cdr.cdr_dstcompanytype from tbl_CDR_DstCodes cn with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.cdr_recordId = cn.cdr_recordId and cdr.ownerId = cn.ownerId and cdr.fsl_tablecode = cn.ownerType left outer join tbl_DST_Distributors dst with (nolock) on dst.dstId = cdr.fsl_dstId left outer join tbl_CDR_Distributors pcdr with (nolock) on pcdr.fsl_dstId = dst.dstParentCo and pcdr.ownerId = cdr.ownerId and pcdr.fsl_tablecode = cdr.fsl_tablecode where cn.ownerId = and cn.ownerType = and cn.cdr_dstcode = select distinct cdr.cdr_recordId, cdr.cdr_dstName, cdr.cdr_dstcompanytype from tbl_CDR_DstCodes cn with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.cdr_recordId = cn.cdr_recordId and cdr.ownerId = cn.ownerId and cdr.fsl_tablecode = cn.ownerType left outer join tbl_DST_Distributors dst with (nolock) on dst.dstId = cdr.fsl_dstId left outer join tbl_CDR_Distributors pcdr with (nolock) on pcdr.fsl_dstId = dst.dstParentCo and pcdr.ownerId = cdr.ownerId and pcdr.fsl_tablecode = cdr.fsl_tablecode where cn.ownerId = and cn.ownerType = and cn.cdr_dstcode = and cdr.cdr_dstCompanytype = insert into tbl_CDR_Distributors (ownerId, fsl_tableCode, cdr_dstName, cdr_dstAddress1, cdr_dstAddress2, cdr_dstcity, cdr_dststate, cdr_dstzip, cdr_dstCountryId, cdr_dstmarketinggrp, cdr_territoryId, fsl_dstId, recState, setMethod, mfr_bsr_id, crmActive,FocusAccount,classificationId, fseTerritoryIdStatus, cdr_priority, useSysFields, hasSysFields, cdr_dstPath, cdr_dstCode, cdr_dstcompanyType, fsl_mapPriority, fsl_masterlinkYN, cdr_specialAcctType, cdr_redistAcctFor, recordSource ) select ownerId, fsl_tableCode, cdr_dstName, cdr_dstAddress1, cdr_dstAddress2, cdr_dstcity, cdr_dststate, cdr_dstzip, cdr_dstCountryId, cdr_dstmarketinggrp, cdr_territoryId, fsl_dstId, recState, setMethod, mfr_bsr_id, crmActive,FocusAccount, classificationId, fseTerritoryIdStatus, cdr_priority, useSysFields, hasSysFields, cdr_dstPath , '' cdr_dstCode, 'S' cdr_dstcompanyType, 2 fsl_mapPriority, 'N' fsl_masterlinkYN, 'RDA' cdr_specialAcctType, #qmd_redistCustomerDetails.cdr_recordId# cdr_redistAcctFor, '' recordSource from tbl_CDR_Distributors redist with (nolock) where ownerId = and fsl_tablecode = and cdr_recordId = select max(cdr_recordId) as cdr_recordId from tbl_CDR_Distributors with (nolock) where ownerId = and fsl_tablecode = and cdr_redistAcctFor = and cdr_dstcompanyType = 'S' update tbl_CDR_Distributors set cdr_dstCode = , recordSource = where cdr_recordId = and ownerId = and fsl_tableCode = insert into tbl_CDR_DstCodes (ownerId, ownerType, cdr_recordId, cdr_dstCode, cdr_dstcode_type, cdr_dstCode_comments) values ( , , , , , ) update tbl_IMPORT_InvoiceHistory set fseCdr_RecordId = where ownerId = and ownerType = and eai_batchId = and customer_number = and reverseInvoice= and redist_customer_number = `