salesByMonth log {ts '2026-04-08 09:12:12'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6981' ) or ( [mfr_id] = '4280' ) or ( [mfr_id] = '4906' ) or ( [mfr_id] = '7485' ) or ( [mfr_id] = '1463' ) ) and ( ( [cdr_dstPath] like 'Sysco Corporate/Sysco Syracuse%' ) or ( [cdr_dstPath] like 'US Foods Corporate/US Foods Buffalo (2R)%' ) or ( [cdr_dstPath] like 'C.A. Curtze Company/C.A. Curtze Co.%' ) or ( [cdr_dstPath] like 'Glazier''s Packing Company%' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:12:12'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6981' ) or ( [mfr_id] = '4280' ) or ( [mfr_id] = '4906' ) or ( [mfr_id] = '7485' ) or ( [mfr_id] = '1463' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:12:23'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) ) ) , _summary as ( select MFRAbbrev_Name,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by MFRAbbrev_Name,period ) , _dataPivot1 as ( select MFRAbbrev_Name ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by MFRAbbrev_Name ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select MFRAbbrev_Name ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by MFRAbbrev_Name ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:12:27'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6975' ) or ( [mfr_id] = '3071' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:12:43'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:12:52'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( [sku] like '%FS-3161%' ) and ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6975' ) or ( [mfr_id] = '3071' ) ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:13:41'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6975' ) or ( [mfr_id] = '3071' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:13:42'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( [sku] like '%FS-3161%' ) and ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6975' ) or ( [mfr_id] = '3071' ) ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:43:09'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6975' ) or ( [mfr_id] = '3071' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:43:09'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( [sku] like '%FS-3161%' ) and ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6975' ) or ( [mfr_id] = '3071' ) ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:43:24'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '723' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:43:31'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( [cdr_dstName] like '%Siemer Distribution Co, Inc%' ) and ( ( 1 = 1 ) and ( ( ( [mfr_id] = '723' ) ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:43:47'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '723' ) or ( [mfr_id] = '1628' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 09:43:50'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( [cdr_dstName] like '%Siemer Distribution Co, Inc%' ) and ( ( 1 = 1 ) and ( ( ( [mfr_id] = '723' ) or ( [mfr_id] = '1628' ) ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:38:38'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '723' ) or ( [mfr_id] = '1628' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:38:45'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( [cdr_dstName] like '%Siemer Distribution Co, Inc%' ) and ( ( 1 = 1 ) and ( ( ( [mfr_id] = '723' ) or ( [mfr_id] = '1628' ) ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,lastInvoiceCases ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:38:57'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) ) ) , _summary as ( select MFRAbbrev_Name,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by MFRAbbrev_Name,period ) , _dataPivot1 as ( select MFRAbbrev_Name ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by MFRAbbrev_Name ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select MFRAbbrev_Name ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by MFRAbbrev_Name ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:39:00'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6981' ) or ( [mfr_id] = '4280' ) or ( [mfr_id] = '4906' ) or ( [mfr_id] = '7485' ) or ( [mfr_id] = '1463' ) ) and ( ( [cdr_dstPath] like 'Sysco Corporate/Sysco Syracuse%' ) or ( [cdr_dstPath] like 'US Foods Corporate/US Foods Buffalo (2R)%' ) or ( [cdr_dstPath] like 'C.A. Curtze Company/C.A. Curtze Co.%' ) or ( [cdr_dstPath] like 'Glazier''s Packing Company%' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:39:06'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( [MFRAbbrev_Name] like '%Orlando Baking Company%' ) and ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6981' ) or ( [mfr_id] = '4280' ) or ( [mfr_id] = '4906' ) or ( [mfr_id] = '7485' ) or ( [mfr_id] = '1463' ) ) and ( ( [cdr_dstPath] like 'Sysco Corporate/Sysco Syracuse%' ) or ( [cdr_dstPath] like 'US Foods Corporate/US Foods Buffalo (2R)%' ) or ( [cdr_dstPath] like 'C.A. Curtze Company/C.A. Curtze Co.%' ) or ( [cdr_dstPath] like 'Glazier''s Packing Company%' ) ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:39:11'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( [MFRAbbrev_Name] like '%Orlando Baking%' ) and ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6981' ) or ( [mfr_id] = '4280' ) or ( [mfr_id] = '4906' ) or ( [mfr_id] = '7485' ) or ( [mfr_id] = '1463' ) ) and ( ( [cdr_dstPath] like 'Sysco Corporate/Sysco Syracuse%' ) or ( [cdr_dstPath] like 'US Foods Corporate/US Foods Buffalo (2R)%' ) or ( [cdr_dstPath] like 'C.A. Curtze Company/C.A. Curtze Co.%' ) or ( [cdr_dstPath] like 'Glazier''s Packing Company%' ) ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:39:16'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( [MFRAbbrev_Name] like '%Orange%' ) and ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6981' ) or ( [mfr_id] = '4280' ) or ( [mfr_id] = '4906' ) or ( [mfr_id] = '7485' ) or ( [mfr_id] = '1463' ) ) and ( ( [cdr_dstPath] like 'Sysco Corporate/Sysco Syracuse%' ) or ( [cdr_dstPath] like 'US Foods Corporate/US Foods Buffalo (2R)%' ) or ( [cdr_dstPath] like 'C.A. Curtze Company/C.A. Curtze Co.%' ) or ( [cdr_dstPath] like 'Glazier''s Packing Company%' ) ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:39:22'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '6981' ) or ( [mfr_id] = '4280' ) or ( [mfr_id] = '4906' ) or ( [mfr_id] = '7485' ) or ( [mfr_id] = '1463' ) ) and ( ( [cdr_dstPath] like 'Sysco Corporate/Sysco Syracuse%' ) or ( [cdr_dstPath] like 'US Foods Corporate/US Foods Buffalo (2R)%' ) or ( [cdr_dstPath] like 'C.A. Curtze Company/C.A. Curtze Co.%' ) or ( [cdr_dstPath] like 'Glazier''s Packing Company%' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:39:38'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '723' ) or ( [mfr_id] = '1628' ) ) and ( ( [cdr_dstPath] like 'Sysco Corporate/Sysco Syracuse%' ) or ( [cdr_dstPath] like 'US Foods Corporate/US Foods Buffalo (2R)%' ) or ( [cdr_dstPath] like 'C.A. Curtze Company/C.A. Curtze Co.%' ) or ( [cdr_dstPath] like 'Glazier''s Packing Company%' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:39:41'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( [cdr_dstName] like '%Siemer Distribution Co, Inc%' ) and ( ( 1 = 1 ) and ( ( ( [mfr_id] = '723' ) or ( [mfr_id] = '1628' ) ) and ( ( [cdr_dstPath] like 'Sysco Corporate/Sysco Syracuse%' ) or ( [cdr_dstPath] like 'US Foods Corporate/US Foods Buffalo (2R)%' ) or ( [cdr_dstPath] like 'C.A. Curtze Company/C.A. Curtze Co.%' ) or ( [cdr_dstPath] like 'Glazier''s Packing Company%' ) ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:39:44'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '723' ) or ( [mfr_id] = '1628' ) ) and ( ( [cdr_dstPath] like 'Sysco Corporate/Sysco Syracuse%' ) or ( [cdr_dstPath] like 'US Foods Corporate/US Foods Buffalo (2R)%' ) or ( [cdr_dstPath] like 'C.A. Curtze Company/C.A. Curtze Co.%' ) or ( [cdr_dstPath] like 'Glazier''s Packing Company%' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:39:44'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( 1 = 1 ) and ( ( ( [mfr_id] = '723' ) or ( [mfr_id] = '1628' ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2026-04-08 10:39:47'} SQL /* -- {ts '2026-01-01 00:00:00'} - {ts '2026-04-01 00:00:00'} -- {ts '2025-01-01 00:00:00'} - {ts '2025-04-01 00:00:00'} */ declare @periodToProrate datetime; declare @prorate float; declare @currentSalesPeriodStart datetime; declare @currentSalesPeriodEnd datetime; declare @priorSalesPeriodStart datetime; declare @priorSalesPeriodEnd datetime; set @periodToProrate = '2025-04-01' ; set @prorate = 0.142857142857 ; set @currentSalesPeriodStart = '2026-01-01' ; set @currentSalesPeriodEnd = '2026-04-01' ; set @priorSalesPeriodStart = '2025-01-01' ; set @priorSalesPeriodEnd = '2025-04-01' ; with _formattedData as ( select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases, lbs, dollars, alt, commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R2_P' + cast (datediff( month, @currentSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @currentSalesPeriodStart and @currentSalesPeriodEnd ) union all select ownerId,ownerType,cdr_recordId,oneFsId,cdr_dstCode,cdr_dstName,cdr_dstCompanyType,territoryId,territoryName,territoryPath,parent_cdr_recordId,parent_oneFsId,parent_cdr_dstCode,parent_cdr_dstName,parent_cdr_dstCompanyType,skuId,sku,skuDesc,prodId,product,shortDesc,categoryId,catNum,category,prodline_id,productLineCode,productLine,budgetCategoryId,budgetCategory,brand,brandId,salesPeriod,salesPeriodType,cdr_specialAcctType,redist_cdr_dstName,redist_cdr_recordId,redist_oneFsId,buyingGroupId,buyingGroup,purchasingMethod,customerType,discontinued,fiscalYear,fiscalMonth,cdr_dstPath,regionName,divisionName,accountOwner_email,accountOwner_fspro_userId,accountOwner_firstName,accountOwner_lastName,cdr_dstAddress1,cdr_dstCity,cdr_dstState,cdr_dstZip,privateLabelType,focusProduct,productHierarchyPath,budgetCategoryMgr_fspro_userId,budgetCategoryMgr_email,budgetCategoryMgr_firstName,budgetCategoryMgr_lastName,realloc,realloc_cdr_recordId,mfr_id,mfr_name,MFRAbbrev_Name,sourceOfData,lastInvoiceDate,lastInvoiceCases,lastInvoiceLbs,lastInvoiceDollars,classificationId,classification,buyingGroupId2,buyingGroup2,buyingGroupId3,buyingGroup3,accountOwner_ownerId,accountOwner_ownerType,territoryNbr,productSetTags,l2ProductHierarchyPath,l3ProductHierarchyPath,l4ProductHierarchyPath,customerSKU,customerSKUType, cases * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as cases, lbs * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as lbs, dollars * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as dollars, alt * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as alt, commission * case when salesPeriod = @periodToProrate then @prorate when salesPeriod between @periodToProRate and @priorSalesPeriodEnd then 0.0 else 1.0 end as commission, active_pipeline_cases, active_pipeline_lbs, active_pipeline_dollars, sold_pipeline_cases, sold_pipeline_lbs, sold_pipeline_dollars, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catnum + ' - ' + category as categoryFull, productLineCode + ' - ' + productLine as productLineFull, 'R1_P' + cast (datediff( month, @priorSalesPeriodStart, salesPeriod ) + 1 as varchar) as period, floor( 10191817 * sqrt( cdr_recordId * log10( cdr_recordId ))) as cdr_recordId_tk, case when parent_cdr_recordId is not null then floor( 10191817 * sqrt( parent_cdr_recordId * log10( parent_cdr_recordId ))) else null end as parent_cdr_recordId_tk, accountOwner_firstName + ' ' + accountOwner_lastName as accountOwner_fullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, concat_ws( '; ', etl.buyingGroup, etl.buyingGroup2, etl.buyingGroup3 ) as allBuyingGroups from tbl_DW_DistributorSales_BRO387 etl with (nolock) where salesPeriodType = 'MONTH' and ( salesPeriod between @priorSalesPeriodStart and @priorSalesPeriodEnd ) ) , _filtered as ( select * from _formattedData where ( ( [cdr_dstName] like '%Siemer Distribution Co, Inc%' ) and ( ( 1 = 1 ) and ( ( ( [mfr_id] = '723' ) or ( [mfr_id] = '1628' ) ) ) ) ) ) , _summary as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period, sum( cases ) as cases, sum( lbs) as lbs, sum( dollars ) as dollars, sum( alt ) as alt, sum( commission ) as commission, sum( active_pipeline_cases ) as active_pipeline_cases, sum( active_pipeline_lbs ) as active_pipeline_lbs, sum( active_pipeline_dollars ) as active_pipeline_dollars, sum( sold_pipeline_cases ) as sold_pipeline_cases, sum( sold_pipeline_lbs ) as sold_pipeline_lbs, sum( sold_pipeline_dollars ) as sold_pipeline_dollars from _filtered group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate,period ) , _dataPivot1 as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( case when period = 'R1_P1' then cases else null end ) as R1_P1 ,sum( case when period = 'R2_P1' then cases else null end ) as R2_P1 ,sum( case when period = 'R1_P1' then active_pipeline_cases else null end ) as A1_P1 ,sum( case when period = 'R2_P1' then active_pipeline_cases else null end ) as A2_P1 ,sum( case when period = 'R1_P1' then sold_pipeline_cases else null end ) as S1_P1 ,sum( case when period = 'R2_P1' then sold_pipeline_cases else null end ) as S2_P1 ,sum( case when period = 'R1_P2' then cases else null end ) as R1_P2 ,sum( case when period = 'R2_P2' then cases else null end ) as R2_P2 ,sum( case when period = 'R1_P2' then active_pipeline_cases else null end ) as A1_P2 ,sum( case when period = 'R2_P2' then active_pipeline_cases else null end ) as A2_P2 ,sum( case when period = 'R1_P2' then sold_pipeline_cases else null end ) as S1_P2 ,sum( case when period = 'R2_P2' then sold_pipeline_cases else null end ) as S2_P2 ,sum( case when period = 'R1_P3' then cases else null end ) as R1_P3 ,sum( case when period = 'R2_P3' then cases else null end ) as R2_P3 ,sum( case when period = 'R1_P3' then active_pipeline_cases else null end ) as A1_P3 ,sum( case when period = 'R2_P3' then active_pipeline_cases else null end ) as A2_P3 ,sum( case when period = 'R1_P3' then sold_pipeline_cases else null end ) as S1_P3 ,sum( case when period = 'R2_P3' then sold_pipeline_cases else null end ) as S2_P3 ,sum( case when period = 'R1_P4' then cases else null end ) as R1_P4 ,sum( case when period = 'R2_P4' then cases else null end ) as R2_P4 ,sum( case when period = 'R1_P4' then active_pipeline_cases else null end ) as A1_P4 ,sum( case when period = 'R2_P4' then active_pipeline_cases else null end ) as A2_P4 ,sum( case when period = 'R1_P4' then sold_pipeline_cases else null end ) as S1_P4 ,sum( case when period = 'R2_P4' then sold_pipeline_cases else null end ) as S2_P4 from [_summary] with (nolock) group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _dataPivot as ( select *, coalesce( R1_P1, 0.0 )+coalesce( R1_P2, 0.0 )+coalesce( R1_P3, 0.0 )+coalesce( R1_P4, 0.0 ) as R1, coalesce( R2_P1, 0.0 )+coalesce( R2_P2, 0.0 )+coalesce( R2_P3, 0.0 )+coalesce( R2_P4, 0.0 ) as R2, coalesce( A1_P1, 0.0 )+coalesce( A1_P2, 0.0 )+coalesce( A1_P3, 0.0 )+coalesce( A1_P4, 0.0 ) as A1, coalesce( A2_P1, 0.0 )+coalesce( A2_P2, 0.0 )+coalesce( A2_P3, 0.0 )+coalesce( A2_P4, 0.0 ) as A2, coalesce( S1_P1, 0.0 )+coalesce( S1_P2, 0.0 )+coalesce( S1_P3, 0.0 )+coalesce( S1_P4, 0.0 ) as S1, coalesce( S2_P1, 0.0 )+coalesce( S2_P2, 0.0 )+coalesce( S2_P3, 0.0 )+coalesce( S2_P4, 0.0 ) as S2 from _dataPivot1 ) , _preData as ( select cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ,sum( R1_P1 ) as R1_P1 ,sum( R2_P1 ) as R2_P1 ,sum( A1_P1 ) as A1_P1 ,sum( A2_P1 ) as A2_P1 ,sum( S1_P1 ) as S1_P1 ,sum( S2_P1 ) as S2_P1 ,sum( R1_P2 ) as R1_P2 ,sum( R2_P2 ) as R2_P2 ,sum( A1_P2 ) as A1_P2 ,sum( A2_P2 ) as A2_P2 ,sum( S1_P2 ) as S1_P2 ,sum( S2_P2 ) as S2_P2 ,sum( R1_P3 ) as R1_P3 ,sum( R2_P3 ) as R2_P3 ,sum( A1_P3 ) as A1_P3 ,sum( A2_P3 ) as A2_P3 ,sum( S1_P3 ) as S1_P3 ,sum( S2_P3 ) as S2_P3 ,sum( R1_P4 ) as R1_P4 ,sum( R2_P4 ) as R2_P4 ,sum( A1_P4 ) as A1_P4 ,sum( A2_P4 ) as A2_P4 ,sum( S1_P4 ) as S1_P4 ,sum( S2_P4 ) as S2_P4 , sum( R1 ) as R1, sum( R2 ) as R2, sum( A1 ) as A1, sum( A2 ) as A2, sum( S1 ) as S1, sum( S2 ) as S2 from _dataPivot group by cdr_dstCode,cdr_dstName,cdr_recordId,cdr_dstPath,oneFsId,MFRAbbrev_Name,sku,customerSKU,lastInvoiceDate ) , _data as ( select *, R2 - R1 as variance, case when R1 != 0 then ( R2 - R1 ) / R1 else null end as variancePct, case when sum( R1 ) over() != 0 then R1 / sum( R1 ) over() else null end as pctOfTotal_R1, case when sum( R2 ) over() != 0 then R2 / sum( R2 ) over() else null end as pctOfTotal_R2 from _preData ) , _return as ( select * from _data ) , _result as ( select ( select count(*) from _data ) as totalCount, ( select * from _return for json auto, include_null_values ) as data ) select * from _result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER