March 20, 2013 at 12:51 pm
sometimes a sp can get out of hand if this if that etc where I saw a developer go over 9k lines.
allot of the code was re-use, the fields, the joins etc, just the where statement would change
so in all the sp just declares the fields and join in two varchar variables
the ifs at the top build the if in 8950 less lines of code. Just by consolidating, I noticed she made so many mistakes because at this length it becomes almost unmanageable.
and just ends up doing
exec @sql1 + @sql2 + @sql3
my question is performance, is this now an uncompiled TSQL statement and less powerful if all the code was "if" out naturally
P.S. I also noticed a downfall with exec, reporting services cannot pickup the fields and you have to either do it manually or cheat and put a sql statement up top and then change it on the backend
March 20, 2013 at 1:00 pm
xgcmcbain (3/20/2013)
sometimes a sp can get out of hand if this if that etc where I saw a developer go over 9k lines.allot of the code was re-use, the fields, the joins etc, just the where statement would change
so in all the sp just declares the fields and join in two varchar variables
the ifs at the top build the if in 8950 less lines of code. Just by consolidating, I noticed she made so many mistakes because at this length it becomes almost unmanageable.
and just ends up doing
exec @sql1 + @sql2 + @sql3
my question is performance, is this now an uncompiled TSQL statement and less powerful if all the code was "if" out naturally
P.S. I also noticed a downfall with exec, reporting services cannot pickup the fields and you have to either do it manually or cheat and put a sql statement up top and then change it on the backend
You have a stored proc that has 9k lines of dynamic sql?
This is so unclear I don't even understand the question.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 20, 2013 at 1:18 pm
no it had 9k lines of
IF(@End_date is null)
BEGIN
END
ELSE
BEGIN
ELSE
BEGIN
etc
I changed it to
set @sql1 = 'select field,field field field'
set @sql2 = 'from table inner join tableb b on b.id = a.id'
if(@end_date is null)
begin
set @sql3 = ' and end_date is null'
end
you have it the opposite
she had 9k lines of pure sql
I got rid of the redundancy in the select and the inner join and whats in every where statement, and simply put up top the if's where more manageable.
USE [DKNY_R1CRM]
GO
/****** Object: StoredProcedure [dbo].[sproc_Report31_Customer_ChangeCume_Quarterly_LastPurchase_V3] Script Date: 3/20/2013 3:15:33 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sproc_Report31_Customer_ChangeCume_Quarterly_LastPurchase_V3]
@sku_id VARCHAR(50)
,@database_brand VARCHAR(10)
,@region VARCHAR(10)
,@store_no VARCHAR(10)
,@from_transaction_date DATETIME
,@to_transaction_date DATETIME
,@freq_report VARCHAR(10)
,@from_amount FLOAT
,@to_amount FLOAT
,@cume_overall_segment VARCHAR(20)
,@product_category tinyint
,@product_department tinyint
,@product_class tinyint
,@address_type VARCHAR(50)
,@user_authorization_level varchar(5)
,@dc_store_brand varchar(10)
,@user_role_store_no varchar(30)
WITH RECOMPILE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--SELECT DISTINCT a.r1_id,a.database_brand, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code, a.email, a.phone, b.address_type, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS, a.last_salesperson_first_name, a.last_salesperson_last_name, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1,a.last_transaction_amount,a.last_transaction_date FROM CRM_QDF A with(nolock) LEFT JOIN CRM_QDF_ADDRESS_TYPE B with(nolock) ON a.R1_ID = b.R1_ID AND a.database_brand = b.database_brand LEFT JOIN XREF_ADHOC_CUME_OVERALL_SEGMENT y with(nolock) ON a.cume_ovr_segment = y.cume_ovr_segment AND a.database_brand = y.database_brand LEFT JOIN CRM_CUST_OLINE Z with(nolock) ON a.r1_id = z.r1_id AND a.cust_row_id = z.cust_row_id AND a.cust_ohead_row_id = z.cust_ohead_row_id LEFT JOIN (SELECT DISTINCT top(5000) aa.r1_id FROM CRM_QDF AA with(nolock) INNER JOIN CRM_QDF_ADDRESS_TYPE BB with(nolock) ON aa.R1_ID = bb.R1_ID AND aa.database_brand = bb.database_brand INNER JOIN XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock) ON aa.cume_ovr_segment = yy.cume_ovr_segment AND aa.database_brand = yy.database_brand LEFT JOIN CRM_CUST_OLINE ZZ with(nolock) ON aa.r1_id = zz.r1_id AND aa.cust_row_id = zz.cust_row_id AND aa.cust_ohead_row_id = zz.cust_ohead_row_id WHERE aa.database_brand = 'DK' AND aa.region = 'US' AND aa.transaction_date BETWEEN 'Jan 1 2012 12:00AM' AND 'Sep 15 2012 12:00AM' AND aa.r1_total_net_retail_central BETWEEN 0 AND 1e+013 AND zz.R1_CATEGORY_ID = 1 AND zz.R1_DEPT_ID = 1 AND zz.R1_CLASS_ID = 1 ) itself ON a.r1_id = itself.r1_id WHERE a.database_brand = 'DK' AND a.region = 'US' AND a.transaction_date BETWEEN 'Jan 1 2012 12:00AM' AND 'Sep 15 2012 12:00AM' AND a.r1_total_net_retail_central BETWEEN 0 AND 1e+013 AND z.[line_object] IN (100, 102, 209, 210) AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99) AND z.R1_CATEGORY_ID = 1 AND z.R1_DEPT_ID = 1 AND z.R1_CLASS_ID = 1 ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2, a.last_transaction_amount desc,a.transaction_date desc , a.store_no, a.cust_ohead_row_id, a.last_salesperson_last_name desc
DECLARE @search_sku_id VARCHAR(10)
, @search_cume_spend_range VARCHAR(10)
, @search_product_category VARCHAR(50)
, @search_freq_report VARCHAR(10)
, @search_store_no VARCHAR(10)
, @top INT
, @sql varchar(MAX)
, @sql1 varchar(MAX)
, @sql2 varchar(MAX)
, @sql3 varchar(MAX)
, @sql4 varchar(MAX)
, @sqljoin varchar(MAX)
SET @search_sku_id = CASE WHEN ISNULL(@sku_id, '') = 'ALL' THEN 'ALL_SKU' ELSE 'EACH_SKU' END
SET @search_cume_spend_range = CASE WHEN ISNULL(@cume_overall_segment, '') = 'ALL' THEN 'ALL_CUME' ELSE 'EACH_CUME' END
SET @search_cume_spend_range = CASE WHEN ISNULL(@cume_overall_segment, '') = 'ALL' THEN 'ALL_CUME' ELSE 'EACH_CUME' END
SET @search_product_category = CASE
WHEN ISNULL(@product_category, 0) = 0 THEN 'ALL_PRODUCT_CATEGORY'
ELSE 'EACH_PRODUCT_CATEGORY' END
SET @search_freq_report = CASE WHEN ISNULL(@freq_report, '') = 'ALL' THEN 'ALL_FREQ' ELSE 'EACH_FREQ' END
SET @search_store_no = CASE WHEN ISNULL(@store_no, '') = '_blank' THEN 'NULL_STORE' ELSE
CASE WHEN ISNULL(@store_no, '') = 'ALL' THEN 'ALL_STORE' ELSE 'EACH_STORE' END END
SET @top = 5000
SET @sql1 = ' '
set @sql = ' '
set @sql2 = ' '
set @sql3 = ' '
set @sql4 = ' '
IF @user_authorization_level in ('0','1','100') -- Corporate Level
BEGIN -- only level to not have store_no in where clause
if @search_store_no = 'NULL_STORE'
BEGIN
set @sql = @sql + ' AND a.store_no is null'
set @sql2 = @sql2 + ' AND aa.store_no = is null'
END
if @search_store_no = 'EACH_STORE'
BEGIN
set @sql = @sql + ' AND a.store_no = ''' + @store_no + ''''
set @sql2 = @sql2 + ' AND aa.store_no = ''' + @store_no + ''''
END
END
ELSE
BEGIN
if @search_store_no = 'NULL_STORE'
BEGIN
set @sql = @sql + ' AND a.store_no is null'
set @sql2 = @sql2 + ' AND aa.store_no = is null'
END
ELSE
BEGIN
set @sql = @sql + ' AND a.store_no = ''' + @store_no + ''''
set @sql2 = @sql2 + ' AND aa.store_no = ''' + @store_no + ''''
END
END
IF @search_freq_report = 'EACH_FREQ'
BEGIN
set @sql = @sql + ' AND a.freq_report = ''' + @freq_report + ''''
set @sql2 = @sql2 + ' AND aa.freq_report = ''' + @freq_report + ''''
END
IF @search_sku_id = 'EACH_SKU'
BEGIN
set @sql = @sql + ' AND z.sku_id = ''' + @sku_id + ''''
set @sql2 = @sql2 + ' AND zz.sku_id = ''' + @sku_id + ''''
END
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN
set @sql = @sql + ' ANDy.cume_ovr_segment = ''' + @cume_overall_segment + ''''
set @sql2 = @sql2 + ' AND yy.cume_ovr_segment = ''' + @cume_overall_segment + ''''
END
if @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN
set @sql = @sql + ' AND z.R1_CATEGORY_ID = ' + CAST(@product_category as varchar(8)) + ' '
set @sql2 = @sql2 + ' AND zz.R1_CATEGORY_ID = ' + CAST(@product_category as varchar(8)) + ' '
END
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN
set @sql = @sql + ' AND (z.R1_CATEGORY_LABEL = '''' OR z.R1_CATEGORY_LABEL IS NULL)'
set @sql2 = @sql2 + ' AND (zz.R1_CATEGORY_LABEL = '''' OR zz.R1_CATEGORY_LABEL IS NULL)'
END
if not @product_department = 0
BEGIN
set @sql = @sql + ' AND z.R1_DEPT_ID = ' + CAST(@product_department as varchar(8)) + ' '
set @sql2 = @sql2 + ' AND zz.R1_DEPT_ID = ' + CAST(@product_department as varchar(8)) + ' '
END
if not @product_class = 0
BEGIN
set @sql = @sql + ' AND z.R1_CLASS_ID = ' + CAST(@product_class as varchar(8)) + ' '
set @sql2 = @sql2 + ' AND zz.R1_CLASS_ID = ' + CAST(@product_class as varchar(8)) + ' '
END
if NOT @address_type = 'All'
BEGIN
set @sqljoin = ' LEFT JOIN XREF_ADHOC_ADDRESS_TYPE XX with(nolock) ON bb.address_type = xx.address_type '
END
set @sql1 = 'SELECT DISTINCT a.r1_id,a.database_brand, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code, a.email, a.phone, b.address_type, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS, a.last_salesperson_first_name, a.last_salesperson_last_name, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1,a.last_transaction_amount,a.last_transaction_date FROM CRM_QDF A with(nolock) LEFT JOIN CRM_QDF_ADDRESS_TYPE B with(nolock) ON a.R1_ID = b.R1_ID AND a.database_brand = b.database_brand LEFT JOIN XREF_ADHOC_CUME_OVERALL_SEGMENT y with(nolock) ON a.cume_ovr_segment = y.cume_ovr_segment AND a.database_brand = y.database_brand LEFT JOIN CRM_CUST_OLINE Z with(nolock) ON a.r1_id = z.r1_id AND a.cust_row_id = z.cust_row_id AND a.cust_ohead_row_id = z.cust_ohead_row_id'
if(len(@sqljoin) > 0)
BEGIN
set @sql3 = ' LEFT JOIN (SELECT DISTINCT top(' + CAST(@top as varchar(15)) + ') aa.r1_id FROM CRM_QDF AA with(nolock) LEFT JOIN CRM_QDF_ADDRESS_TYPE BB with(nolock) ON aa.R1_ID = bb.R1_ID AND aa.database_brand = bb.database_brand LEFT JOIN XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock) ON aa.cume_ovr_segment = yy.cume_ovr_segment AND aa.database_brand = yy.database_brand LEFT JOIN CRM_CUST_OLINE ZZ with(nolock) ON aa.r1_id = zz.r1_id AND aa.cust_row_id = zz.cust_row_id AND aa.cust_ohead_row_id = zz.cust_ohead_row_id ' + @sqljoin + ' WHERE aa.database_brand = ''' + @database_brand + ''' AND aa.region = ''' + @region + ''' AND aa.transaction_date BETWEEN ''' + CAST(@from_transaction_date as varchar(50)) + ''' AND ''' + CAST(@to_transaction_date as varchar(50)) + ''' AND aa.r1_total_net_retail_central BETWEEN ' + CAST(@from_amount as varchar(10)) + ' AND ' + CAST(@to_amount as varchar(10))
END
ELSE
BEGIN
set @sql3 = ' LEFT JOIN (SELECT DISTINCT top(' + CAST(@top as varchar(15)) + ') aa.r1_id FROM CRM_QDF AA with(nolock) INNER JOIN CRM_QDF_ADDRESS_TYPE BB with(nolock) ON aa.R1_ID = bb.R1_ID AND aa.database_brand = bb.database_brand INNER JOIN XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock) ON aa.cume_ovr_segment = yy.cume_ovr_segment AND aa.database_brand = yy.database_brand LEFT JOIN CRM_CUST_OLINE ZZ with(nolock) ON aa.r1_id = zz.r1_id AND aa.cust_row_id = zz.cust_row_id AND aa.cust_ohead_row_id = zz.cust_ohead_row_id WHERE aa.database_brand = ''' + @database_brand + ''' AND aa.region = ''' + @region + ''' AND aa.transaction_date BETWEEN ''' + CAST(@from_transaction_date as varchar(50)) + ''' AND ''' + CAST(@to_transaction_date as varchar(50)) + ''' AND aa.r1_total_net_retail_central BETWEEN ' + CAST(@from_amount as varchar(10)) + ' AND ' + CAST(@to_amount as varchar(10))
END
set @sql4 =' WHERE a.database_brand = ''' + @database_brand + ''' AND a.region = ''' + @region + ''' AND a.transaction_date BETWEEN ''' + CAST(@from_transaction_date as varchar(50)) + ''' AND ''' + CAST(@to_transaction_date as varchar(50)) + ''' AND a.r1_total_net_retail_central BETWEEN ' + CAST(@from_amount as varchar(8)) + ' AND ' + CAST(@to_amount as varchar(8))+ ' AND z.[line_object] IN (100, 102, 209, 210) AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99) '
if NOT @address_type = 'All'
BEGIN
set @sql1 = @sql1 + ' LEFT JOIN XREF_ADHOC_ADDRESS_TYPE x with(nolock) ON b.address_type = x.address_type '
set @sql3 = @sql3 + ' AND bb.address_type = ''' + @address_type + ''''
set @sql4 = @sql4 + ' AND b.address_type = ''' + @address_type + ''''
END
set @sql1 = @sql1 + @sql3
if(LEN(@sql2) > 0)
BEGIN
set @sql1 = @sql1 + @sql2
END
set @sql1 = @sql1 + ') itself ON a.r1_id = itself.r1_id' + @sql4
if(LEN(@sql) > 0)
BEGIN
set @sql1 = @sql1 + @sql
END
set @sql1 = @sql1 + ' ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2, a.last_transaction_amount desc,a.transaction_date desc , a.store_no, a.cust_ohead_row_id, a.last_salesperson_last_name desc'
exec (@sql1)
END
here old way
USE [DKNY_R1CRM]
GO
/****** Object: StoredProcedure [dbo].[sproc_Report31_Customer_ChangeCume_Quarterly_LastPurchase] Script Date: 3/20/2013 3:18:19 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:<Author,,Name>
-- Create date: <Create Date,,>
-- Description:<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sproc_Report31_Customer_ChangeCume_Quarterly_LastPurchase]
@sku_id VARCHAR(50)
,@database_brand VARCHAR(10)
,@region VARCHAR(10)
,@store_no VARCHAR(10)
,@from_transaction_date DATETIME
,@to_transaction_date DATETIME
,@freq_report VARCHAR(10)
,@from_amount FLOAT
,@to_amount FLOAT
,@cume_overall_segment VARCHAR(20)
,@product_category VARCHAR(50)
,@address_type VARCHAR(50)
,@user_authorization_level varchar(5)
,@dc_store_brand varchar(10)
,@user_role_store_no varchar(30)
WITH RECOMPILE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @search_sku_id VARCHAR(10)
, @search_cume_spend_range VARCHAR(10)
, @search_product_category VARCHAR(50)
, @search_freq_report VARCHAR(10)
, @search_store_no VARCHAR(10)
, @top INT
SET @search_sku_id = CASE WHEN ISNULL(@sku_id, '') = 'ALL' THEN 'ALL_SKU' ELSE 'EACH_SKU' END
SET @search_cume_spend_range = CASE WHEN ISNULL(@cume_overall_segment, '') = 'ALL' THEN 'ALL_CUME' ELSE 'EACH_CUME' END
SET @search_product_category = CASE WHEN ISNULL(@product_category,'') = '' THEN 'NULL_PRODUCT_CATEGORY' ELSE
CASE WHEN ISNULL(@product_category, '') = 'ALL' THEN 'ALL_PRODUCT_CATEGORY'
ELSE 'EACH_PRODUCT_CATEGORY' END END
SET @search_freq_report = CASE WHEN ISNULL(@freq_report, '') = 'ALL' THEN 'ALL_FREQ' ELSE 'EACH_FREQ' END
SET @search_store_no = CASE WHEN ISNULL(@store_no, '') = '_blank' THEN 'NULL_STORE' ELSE
CASE WHEN ISNULL(@store_no, '') = 'ALL' THEN 'ALL_STORE' ELSE 'EACH_STORE' END END
SET @top = 200
IF @user_authorization_level in ('0','1','100') -- Corporate Level
BEGIN -- @user_authorization_level in ('0','1','100') -- Corporate Level
IF @search_store_no = 'ALL_STORE'
BEGIN -- ALL_STORE
IF @search_freq_report = 'ALL_FREQ'
BEGIN -- ALL_STORE, ALL_FREQ
IF @search_sku_id = 'ALL_SKU'
BEGIN -- ALL_STORE, ALL_FREQ, ALL_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- ALL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN ( /* TS commented on 05/31/12 - we put these INNER JOIN to itself becasue
we need to limit how many CUSTOMER records returned (NOT transactions) */
SELECT DISTINCT top(200)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc , a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
/*DECLARE @sku_id VARCHAR(50)
,@database_brand VARCHAR(10)
,@region VARCHAR(10)
,@store_no VARCHAR(10)
,@from_transaction_date DATETIME
,@to_transaction_date DATETIME
,@freq_report VARCHAR(10)
,@from_amount FLOAT
,@to_amount FLOAT
,@cume_overall_segment VARCHAR(20)
,@product_category VARCHAR(50)
,@address_type VARCHAR(50)
,@user_authorization_level varchar(5)
,@dc_store_brand varchar(10)
,@user_role_store_no varchar(30)
set @sku_id ='ALL'
set @database_brand ='DK'
set @region = 'US'
set @store_no = 'ALL'
set @from_transaction_date = '2/12/2011'
set @to_transaction_date = '7/2/2012'
set @freq_report = 'ALL'
set @from_amount = 0
set @to_amount = 1000000
set @cume_overall_segment = 'ALL'
set @product_category = 'ALL'
set @address_type = 'Physical_Address'*/
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock) --with(nolock, index (IX_CRM_QDF_2))
inner JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock) --with(nolock,index(IX_CRM_QDF_ADDRESS_TYPE_R1ID_DBBRAND))
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
inner JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
inner JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock) --with(nolock,index(IX_XREF_ADHOC_CUME_OVERALL_SEGMENT))
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock) --with(nolock,index(IX_CRM_CUST_OLINE_SP_CustomerLooup_TransactionDetail_Revised))
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
inner JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock) --with(nolock, index (IX_CRM_QDF_2))
inner JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock) --with(nolock,index(IX_CRM_QDF_ADDRESS_TYPE_R1ID_DBBRAND))
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
inner JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX
ON bb.address_type = xx.address_type
inner JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock) --with(nolock,index(IX_XREF_ADHOC_CUME_OVERALL_SEGMENT))
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock) --with(nolock,index(IX_CRM_CUST_OLINE_SP_CustomerLooup_TransactionDetail_Revised))
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand AND aa.region IN (@region) AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- ALL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- ALL_FREQ, ALL_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND y.cume_ovr_segment IN (@cume_overall_segment)
AND (z.R1_CATEGORY_LABEL IN (@product_category))
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- ALL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME
END -- ALL_STORE, ALL_FREQ, ALL_SKU
ELSE
BEGIN -- ALL_STORE, ALL_FREQ, EACH_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- ALL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- ALL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- ALL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- ALL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME
END -- ALL_STORE, ALL_FREQ, EACH_SKU
END -- ALL_STORE, ALL_FREQ, ALL_FREQ
--------------------------***********************************************************
ELSE
IF @search_freq_report = 'EACH_FREQ'
BEGIN -- ALL_STORE, EACH_FREQ
IF @search_sku_id = 'ALL_SKU'
BEGIN -- ALL_STORE, EACH_FREQ, ALL_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- ALL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.[line_object] IN (100, 102, 209, 210)
AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.[line_object] IN (100, 102, 209, 210)
AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock) ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- ALL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- ALL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- ALL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME
END -- ALL_STORE, EACH_FREQ, ALL_SKU
ELSE
BEGIN -- ALL_STORE, EACH_FREQ, EACH_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- ALL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- ALL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- ALL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- ALL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- ALL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME
END -- ALL_STORE, EACH_FREQ, EACH_SKU
END -- ALL_STORE, EACH_FREQ
END -- ALL_STORE
--- =================================================================
ELSE
IF @search_store_no = 'EACH_STORE'
BEGIN -- EACH_STORE
IF @search_freq_report = 'ALL_FREQ'
BEGIN -- EACH_STORE, ALL_FREQ
IF @search_sku_id = 'ALL_SKU'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN ( /* TS commented on 05/31/12 - we put these INNER JOIN to itself becasue
we need to limit how many CUSTOMER records returned (NOT transactions) */
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.[line_object] IN (100, 102, 209, 210)
AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.[line_object] IN (100, 102, 209, 210)
AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME
END -- EACH_STORE, ALL_FREQ, ALL_SKU
ELSE
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME
END -- EACH_STORE, ALL_FREQ, EACH_SKU
END -- EACH_STORE, ALL_FREQ, ALL_FREQ
--------------------------***********************************************************
ELSE
IF @search_freq_report = 'EACH_FREQ'
BEGIN -- EACH_STORE, EACH_FREQ
IF @search_sku_id = 'ALL_SKU'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.[line_object] IN (100, 102, 209, 210)
AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.[line_object] IN (100, 102, 209, 210)
AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME
END -- EACH_STORE, EACH_FREQ, ALL_SKU
ELSE
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME
END -- EACH_STORE, EACH_FREQ, EACH_SKU
END -- EACH_STORE, EACH_FREQ
END -- EACH_STORE
------ NULL_STORE
--- =================================================================
ELSE
IF @search_store_no = 'NULL_STORE'
BEGIN -- NULL_STORE
IF @search_freq_report = 'ALL_FREQ'
BEGIN -- NULL_STORE, ALL_FREQ
IF @search_sku_id = 'ALL_SKU'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN ( /* TS commented on 05/31/12 - we put these INNER JOIN to itself becasue
we need to limit how many CUSTOMER records returned (NOT transactions) */
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.[line_object] IN (100, 102, 209, 210)
AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.[line_object] IN (100, 102, 209, 210)
AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME
END -- NULL_STORE, ALL_FREQ, ALL_SKU
ELSE
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME
END -- NULL_STORE, ALL_FREQ, EACH_SKU
END -- NULL_STORE, ALL_FREQ, ALL_FREQ
--------------------------***********************************************************
ELSE
IF @search_freq_report = 'EACH_FREQ'
BEGIN -- NULL_STORE, EACH_FREQ
IF @search_sku_id = 'ALL_SKU'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.[line_object] IN (100, 102, 209, 210)
AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.[line_object] IN (100, 102, 209, 210)
AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME
END -- NULL_STORE, EACH_FREQ, ALL_SKU
ELSE
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME
END -- NULL_STORE, EACH_FREQ, EACH_SKU
END -- NULL_STORE, EACH_FREQ
END -- NULL_STORE
END -- @user_authorization_level in ('0','1','100') -- Corporate Level
ELSE
IF @user_authorization_level = '104' -- Store Level
BEGIN -- @user_authorization_level = '104' -- Store Level
IF @search_store_no = 'EACH_STORE'
BEGIN -- EACH_STORE
IF @search_freq_report = 'ALL_FREQ'
BEGIN -- EACH_STORE, ALL_FREQ
IF @search_sku_id = 'ALL_SKU'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN ( /* TS commented on 05/31/12 - we put these INNER JOIN to itself becasue
we need to limit how many CUSTOMER records returned (NOT transactions) */
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.[line_object] IN (100, 102, 209, 210)
AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.[line_object] IN (100, 102, 209, 210)
AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, ALL_FREQ, ALL_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, ALL_FREQ, ALL_SKU, EACH_CUME
END -- EACH_STORE, ALL_FREQ, ALL_SKU
ELSE
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, ALL_FREQ, EACH_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, ALL_FREQ, EACH_SKU, EACH_CUME
END -- EACH_STORE, ALL_FREQ, EACH_SKU
END -- EACH_STORE, ALL_FREQ, ALL_FREQ
--------------------------***********************************************************
ELSE
IF @search_freq_report = 'EACH_FREQ'
BEGIN -- EACH_STORE, EACH_FREQ
IF @search_sku_id = 'ALL_SKU'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.[line_object] IN (100, 102, 209, 210)
AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.[line_object] IN (100, 102, 209, 210)
AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, EACH_FREQ, ALL_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, EACH_FREQ, ALL_SKU, EACH_CUME
END -- EACH_STORE, EACH_FREQ, ALL_SKU
ELSE
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, EACH_FREQ, EACH_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IN (@store_no)
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IN (@store_no)
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- EACH_STORE, EACH_FREQ, EACH_SKU, EACH_CUME
END -- EACH_STORE, EACH_FREQ, EACH_SKU
END -- EACH_STORE, EACH_FREQ
END -- EACH_STORE
------ NULL_STORE
--- =================================================================
ELSE
IF @search_store_no = 'NULL_STORE'
BEGIN -- NULL_STORE
IF @search_freq_report = 'ALL_FREQ'
BEGIN -- NULL_STORE, ALL_FREQ
IF @search_sku_id = 'ALL_SKU'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN ( /* TS commented on 05/31/12 - we put these INNER JOIN to itself becasue
we need to limit how many CUSTOMER records returned (NOT transactions) */
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.[line_object] IN (100, 102, 209, 210)
AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.[line_object] IN (100, 102, 209, 210)
AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, ALL_FREQ, ALL_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, ALL_FREQ, ALL_SKU, EACH_CUME
END -- NULL_STORE, ALL_FREQ, ALL_SKU
ELSE
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- ALL_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, ALL_FREQ, EACH_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- ALL_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, ALL_FREQ, EACH_SKU, EACH_CUME
END -- NULL_STORE, ALL_FREQ, EACH_SKU
END -- NULL_STORE, ALL_FREQ, ALL_FREQ
--------------------------***********************************************************
ELSE
IF @search_freq_report = 'EACH_FREQ'
BEGIN -- NULL_STORE, EACH_FREQ
IF @search_sku_id = 'ALL_SKU'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.[line_object] IN (100, 102, 209, 210)
AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.[line_object] IN (100, 102, 209, 210)
AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, EACH_FREQ, ALL_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, EACH_FREQ, ALL_SKU, EACH_CUME
END -- NULL_STORE, EACH_FREQ, ALL_SKU
ELSE
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU
IF @search_cume_spend_range = 'ALL_CUME'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, EACH_FREQ, EACH_SKU, ALL_CUME
ELSE
IF @search_cume_spend_range = 'EACH_CUME'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME
IF @search_product_category = 'NULL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL = '' OR zz.R1_CATEGORY_LABEL IS NULL)
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL = '' OR z.R1_CATEGORY_LABEL IS NULL)
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, NULL_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'EACH_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND (zz.R1_CATEGORY_LABEL IN (@product_category))
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND (z.R1_CATEGORY_LABEL IN (@product_category))
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, EACH_PRODUCT_CATEGORY
ELSE
IF @search_product_category = 'ALL_PRODUCT_CATEGORY'
BEGIN -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
SELECT DISTINCT
a.r1_id, a.database_brand
, a.[first], a.[last], a.add1, a.add2, a.city, a.county, a.[state], a.zip, a.country_code
, a.email, a.phone
, b.address_type, x.address_type_description
, a.region, a.freq_report, a.cume_ovr_segment, y.cume_segment_label
, a.cust_ohead_row_id, a.transaction_date, a.store_no, a.register_no
, a.pos_transaction_no, a.pos_transaction_id, a.customer_no, a.r1_total_net_retail_central
, a.crm_customer_no, a.cume_overall, a.cume_12MOS,a.cume_1324MOS
, z.salesperson_first_name, z.salesperson_last_name
, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,5,a.[database_brand]) as Q5, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,4,a.[database_brand]) as Q4, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,3,a.[database_brand]) as Q3, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,2,a.[database_brand]) as Q2, dbo.udf_Rpt31_Customer_Quarterly(a.r1_id,1,a.[database_brand]) as Q1
FROM DKNY_R1CRM.dbo.CRM_QDF A with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE B with(nolock)
ON a.R1_ID = b.R1_ID
AND a.database_brand = b.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE X with(nolock)
ON b.address_type = x.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT Y with(nolock)
ON a.cume_ovr_segment = y.cume_ovr_segment
AND a.database_brand = y.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE Z with(nolock)
ON a.r1_id = z.r1_id
AND a.cust_row_id = z.cust_row_id
AND a.cust_ohead_row_id = z.cust_ohead_row_id
INNER JOIN (
SELECT DISTINCT TOP (@top)
aa.r1_id
FROM DKNY_R1CRM.dbo.CRM_QDF AA with(nolock)
INNER JOIN DKNY_R1CRM.dbo.CRM_QDF_ADDRESS_TYPE BB with(nolock)
ON aa.R1_ID = bb.R1_ID
AND aa.database_brand = bb.database_brand
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_ADDRESS_TYPE XX with(nolock)
ON bb.address_type = xx.address_type
INNER JOIN DKNY_R1CRM.dbo.XREF_ADHOC_CUME_OVERALL_SEGMENT YY with(nolock)
ON aa.cume_ovr_segment = yy.cume_ovr_segment
AND aa.database_brand = yy.database_brand
LEFT JOIN DKNY_R1CRM.dbo.CRM_CUST_OLINE ZZ with(nolock)
ON aa.r1_id = zz.r1_id
AND aa.cust_row_id = zz.cust_row_id
AND aa.cust_ohead_row_id = zz.cust_ohead_row_id
WHERE aa.database_brand = @database_brand
AND aa.region IN (@region)
AND aa.store_no IS NULL
AND aa.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND aa.freq_report IN (@freq_report)
AND aa.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (aa.mailable = 'Y' OR aa.emailable = 'Y')
AND bb.address_type = @address_type
AND zz.sku_id = @sku_id
ANDyy.cume_ovr_segment IN (@cume_overall_segment)
--AND zz.[line_object] IN (100, 102, 209, 210)
--AND zz.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
) itself
ON a.r1_id = itself.r1_id
WHERE a.database_brand = @database_brand
AND a.region IN (@region)
AND a.store_no IS NULL
AND a.transaction_date BETWEEN @from_transaction_date AND @to_transaction_date
AND a.freq_report IN (@freq_report)
AND a.r1_total_net_retail_central BETWEEN @from_amount AND @to_amount
--AND (a.mailable = 'Y' OR a.emailable = 'Y')
AND b.address_type = @address_type
AND z.sku_id = @sku_id
AND y.cume_ovr_segment IN (@cume_overall_segment)
--AND z.[line_object] IN (100, 102, 209, 210)
--AND z.[line_action] IN (1, 7, 24, 90, 101, 102, 2, 8, 99)
ORDER BY a.r1_id, a.database_brand, a.first, a.last, a.add1, a.add2
, a.transaction_date desc, a.store_no, a.cust_ohead_row_id
END -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME, ALL_PRODUCT_CATEGORY
END -- NULL_STORE, EACH_FREQ, EACH_SKU, EACH_CUME
END -- NULL_STORE, EACH_FREQ, EACH_SKU
END -- NULL_STORE, EACH_FREQ
END -- NULL_STORE
END -- @user_authorization_level = '104' -- Store Level
END
which runs faster?
March 20, 2013 at 1:27 pm
You should read this article from Gail.
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/[/url]
Also be VERY careful with all those NOLOCK hints. That hint is incredibly dangerous when you don't understand all the real issues with it. It not a magic go fast pill. It is a magic get possible duplicates and/or missing data which present nearly impossible to reproduce bugs.
Here are a few links...
http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx
http://www.jasonstrate.com/2012/06/the-side-effect-of-nolock/[/url]
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 20, 2013 at 2:07 pm
trust me this business logic allows all nolocks allowed. this is a read only database. its only written to overnight.
Thanks for the answer as it seems the dynamic sql I used is the better choice.
March 20, 2013 at 2:10 pm
xgcmcbain (3/20/2013)
trust me this business logic allows all nolocks allowed. this is a read only database. its only written to overnight.Thanks for the answer as it seems the dynamic sql I used is the better choice.
Then why bother with that hint at all? If there is no chance there are current transaction it doesn't help anyway.
Glad to hear you were able to figure out the solution.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 20, 2013 at 2:22 pm
common practice in my noodle, I say nolock when I don't care if I know its read only or don't want to interfere with anyone, and I don't use it if I want an up to date table, and will wait for an up to date table
March 20, 2013 at 2:43 pm
Dynamic SQL is the better choice in this case - however, the way you have approached this has created a potential issue and opened that procedure up to SQL injection.
You need to rebuild the dynamic SQL so you are using parameters and sp_executeSQL instead of concatenating the string.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
March 20, 2013 at 2:52 pm
xgcmcbain (3/20/2013)
common practice in my noodle, I say nolock when I don't care if I know its read only or don't want to interfere with anyone, and I don't use it if I want an up to date table, and will wait for an up to date table
You should be VERY careful about just tossing around that hint. It is very dangerous. I understand your point but you should instead look at isolation levels. The not interferring part is the worst possible reason to use that hint. I know this is off topic from your original post but that hint is not a good habit.
Also I agree 10000% with Jeffrey that you need to use parameterized dynamic sql. NEVER NEVER NEVER execute dynamic sql that is not parameterized when the values come from your stored proc parameters. The article from Gail that I referenced earlier has good examples of how to do this.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 20, 2013 at 4:28 pm
I 100% agree this can be hacked. I made 1 improvement which I had -3 days to work on. This site has 12 ip's that can access as so you can consider it departmental internal so my alarm to work on security was low.
google my name, I have hacked many things "Michael Evanchik" xp service pack 2, wired magazine , eweek I should know better. But u get what you pay for. This isn't paying for a security overhaul. If they want want I would gladly rewrite.
Same as with using the Hints, this is a read only db. not seeing your point
but again thanks all for the feedback im signing off this thread
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply