SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Proper Way to Count # of Customers


Proper Way to Count # of Customers

Author
Message
rob26r
rob26r
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 80

I've seen many market basket analysis posts but none that address the issues I have.

Here are the requirements:

1) Identify customers who purchased Mountain Bikes or Road Bikes (ProductSubcategory 1 & 2) between 5/1/2018 and 5/15/2018

2) For the customers from above, sort and rank by total Product sales descending and also include the # of customers that purchased (between 5/1/2018 and 6/15/2018) each Product (excluding Products in ProductSubcategory Mountain Bikes or Road Bikes)

This what I have so far. I'm using the AdventureWorksDW 2012 database.


with

------------------------------------------------------------
--Select ProductSubcategories that were purchased
------------------------------------------------------------
SET [ProductSubcategory_Purchased] as
{
[Product].[Product Subcategory Key].&[1],[Product].[Product Subcategory Key].&[2]
}
MEMBER [Product].[Product Subcategory Key].[All].[AggRange] as Aggregate([ProductSubcategory_Purchased])

------------------------------------------------------------
--Select purchase period
------------------------------------------------------------
SET [Purchase_Period] as
{
[Date].[Date].[Date].&[20080401]:[Date].[Date].[Date].&[20080515]
}
MEMBER [Date].[Date].[All].[AggRange] as Aggregate([Purchase_Period])

------------------------------------------------------------
--Select Customers that purchased Mountain Bikes or Road Bikes
------------------------------------------------------------
SET [Customer_Purchase_List] as
filter
(
[Customer].[Customer Key].[Customer Key].members ,
(
[Product].[Product Subcategory Key].[All].[AggRange]
, [Date].[Date].[All].[AggRange]
, [Measures].[Sales Amount]
) > 0
)

MEMBER [Customer].[Customer Key].[All].[AggRange] as Aggregate([Customer_Purchase_List])

------------------------------------------------------------
--Select all ProductSubCategories excluding Mountain Bikes or Road Bikes
------------------------------------------------------------
SET [ProductSubcategory_Other] as
(
[Product].[Product Subcategory Key].Children - [ProductSubcategory_Purchased] - [Product].[Product Subcategory Key].[All].UNKNOWNMEMBER
)
MEMBER [Product].[Product Subcategory Key].[All].[AggRange1] as Aggregate([ProductSubcategory_Other])

------------------------------------------------------------
--Create set with new analysis purchase period
------------------------------------------------------------
SET [Purchase_Period_Other] as
{
[Date].[Date].[Date].&[20080401]:[Date].[Date].[Date].&[20080615]
}
MEMBER [Date].[Date].[All].[AggRangeOther] as Aggregate([Purchase_Period_Other] )

------------------------------------------------------------
--Create measure to get sales data
------------------------------------------------------------
MEMBER [Measures].[Sales Amount Total] as
sum
(
(
[Customer_Purchase_List] , [Date].[Date].[All].[AggRangeOther]
) , [Measures].[Sales Amount]
)


------------------------------------------------------------
--Create set ordering sales by Product
------------------------------------------------------------
SET [Product_Other_Order] AS
order
(
nonempty
(
[Product].[Product Key].[Product Key] * [ProductSubcategory_Other]
)
, [Measures].[Sales Amount Total] , bdesc
)

------------------------------------------------------------
--Create measure actually ranking set above
------------------------------------------------------------
MEMBER measures.[Product_Other_Rank] AS
Rank
(
(
[Product].[Product Key].currentmember , [Product].[Product Subcategory Key].currentmember
)
, ([Product_Other_Order])
)



------------------------------------------------------------
--Count # of customers
------------------------------------------------------------
MEMBER [Measures].[NumCustomers] as

(
(
[Customer_Purchase_List] , [Date].[Date].[All].[AggRangeOther]
) , [Measures].[Sales Amount]
).count

------------------------------------------------------------
--Get final results
------------------------------------------------------------
select
{
{
[Measures].[Sales Amount Total], measures.[Product_Other_Rank] , [Measures].[NumCustomers]
}
} on 0 ,
[Product_Other_Order] having [Measures].[Sales Amount Total] > 0
on 1
from [AdventureWorksDW]


Everything is working except I don't know how to count the # of customers that purchased each product. Any thoughts? Thanks.

Example output:

enter image description here

Thanks for your help!


rob26r
rob26r
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 80
For anyone else, found the solution to my issue.

member [Measures].[NumCustomers] as
count (
filter (
exists([Customer].[Customer Key].[Customer Key].members , {[Customer_Purchase_List]})
, (
[Date].[Date].[All].[AggRangeOther]
, [Measures].[Sales Amount]
) > 0
)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)

Group: General Forum Members
Points: 885078 Visits: 47971

I'm not a GUI programmer nor do I use SSAS, SSIS, SSRS, DW, etc, etc, so I have to ask what "language" is this and why haven't you simply used T-SQL to solve this database problem?



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jason A. Long
Jason A. Long
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20033 Visits: 7981
Jeff Moden - Friday, November 9, 2018 6:40 AM

I'm not a GUI programmer nor do I use SSAS, SSIS, SSRS, DW, etc, etc, so I have to ask what "language" is this and why haven't you simply used T-SQL to solve this database problem?


If I'm not mistaken, that's MDX. It's for talking to SSAS cudes.

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)

Group: General Forum Members
Points: 885078 Visits: 47971
Thanks, Jason.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
rob26r
rob26r
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 80
Correct - MDX. Oops, perhaps I posted this on the wrong forum.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)SSC Guru (885K reputation)

Group: General Forum Members
Points: 885078 Visits: 47971
rob26r - Friday, November 9, 2018 7:29 AM
Correct - MDX. Oops, perhaps I posted this on the wrong forum.


No. MDX code is appropriate for a BI forum (IMHO). When I get some time (maybe this weekend), I'm thinking about taking a crack at it using just plain ol' T-SQL to see if it can be simplified.

p.s. Nice job on commenting and formatting the code.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search