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


Analysing Sales Patterns: R + SQL Server


Analysing Sales Patterns: R + SQL Server

Author
Message
nick.dale.burns
nick.dale.burns
Mr or Mrs. 500
Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)

Group: General Forum Members
Points: 577 Visits: 270
Comments posted to this topic are about the item Analysing Sales Patterns: R + SQL Server
Kyrilluk
Kyrilluk
Old Hand
Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)

Group: General Forum Members
Points: 324 Visits: 357
Exellent article, thank you.
For those of you that just hate creating views or would like to keep experimenting with different sql queries and have windows connection rather than a SQL ones, the following my help:

SQLconnection<-odbcDriverConnect("Driver=SQL Server; Server=MyDummyInstance\\BI_DEV; Database=AdventureWorksDW2012; trusted_connection=true;")

salesData<- sqlQuery(SQLconnection, "
select
sales.ProductKey,
p.ProductSubcategoryKey,
ps.EnglishProductSubcategoryName,
p.EnglishProductName,
sales.SalesTerritoryKey,
t.SalesTerritoryRegion,
sales.CustomerKey
from dbo.FactInternetSales as sales
inner join dbo.DimProduct as p on p.ProductKey = sales.ProductKey
inner join dbo.DimProductSubcategory as ps on ps.ProductSubcategoryKey = p.ProductSubcategoryKey
inner join dbo.DimSalesTerritory as t on t.SalesTerritoryKey = sales.SalesTerritoryKey;
")

odbcClose(SQLconnection)


The are some issues with the code (some missing parenthesis and some reference to data frame or variables that don't exist such as the product_contributions one).
nick.dale.burns
nick.dale.burns
Mr or Mrs. 500
Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)

Group: General Forum Members
Points: 577 Visits: 270
Kyrilluk (12/15/2015)
Excellent article, thank you.
For those of you that just hate creating views or would like to keep experimenting with different sql queries and have windows connection rather than a SQL ones, the following my help:

SQLconnection<-odbcDriverConnect("Driver=SQL Server; Server=MyDummyInstance\\BI_DEV; Database=AdventureWorksDW2012; trusted_connection=true;")

salesData<- sqlQuery(SQLconnection, "
select
sales.ProductKey,
p.ProductSubcategoryKey,
ps.EnglishProductSubcategoryName,
p.EnglishProductName,
sales.SalesTerritoryKey,
t.SalesTerritoryRegion,
sales.CustomerKey
from dbo.FactInternetSales as sales
inner join dbo.DimProduct as p on p.ProductKey = sales.ProductKey
inner join dbo.DimProductSubcategory as ps on ps.ProductSubcategoryKey = p.ProductSubcategoryKey
inner join dbo.DimSalesTerritory as t on t.SalesTerritoryKey = sales.SalesTerritoryKey;
")

odbcClose(SQLconnection)


Very nice - thank you for adding that Smile


Cheers,
Nick
yogmangela
yogmangela
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 55
Hi Nick,

Thanks for the tutorial. Great to see someone putting effort to share their knowledge.

Just a little hickup.
below variables are not assigned for last graph.

- product_contributions
- by_region_trimmed

I am getting error :
> by_region_filter<- with(majorSalesData, table(SalesTerritoryRegion, ProductKey))
> results_filters <- CA(by_region_trimmed, graph = FALSE)
[color=#f00]Error in is.table(X) : object 'by_region_trimmed' not found[/color]
>

I look forward to more of these tutorial.

Thanks,
Yogs
chris.smith 3432
chris.smith 3432
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 2
Hi Nick,

I enjoyed this article very much. I am an Analysis Services user, but I put together some (much simpler) R examples for our R users to access our Data Warehouse. This model is spot on for SQL Server 2012 and 2014.

I am about to build some examples using 2016 CTP3.1 embedding the R calls in SQL scripts. Any thoughts about how you take advantage of the embedded model for the types of analyses in your examples?

Thank you for sharing!

Chris
Iwas Bornready
Iwas Bornready
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21824 Visits: 885
Thanks for the great article.
bteague
bteague
SSC Veteran
SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)

Group: General Forum Members
Points: 243 Visits: 196
Great article and timely as I am currently in the process of explaining the power of R relative to what is currently in the MSBI stack. I might add the built-in visualizations as a differentiator compared to the SSAS basket analysis solution. I've been challenged to investigate options in SSAS, but my intuition tells me that it would be futile given the current flexibility of R, the direction of 2016 and the inevitable granularity required to conduct such analyses.
Alan.B
Alan.B
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12825 Visits: 7996
Great article!

-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
yogmangela
yogmangela
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 55
have you created ODBC connection form Data Source ?
nick.dale.burns
nick.dale.burns
Mr or Mrs. 500
Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)

Group: General Forum Members
Points: 577 Visits: 270
yogmangela (12/15/2015)
Hi Nick,

Thanks for the tutorial. Great to see someone putting effort to share their knowledge.

Just a little hickup.
below variables are not assigned for last graph.

- product_contributions
- by_region_trimmed

I am getting error :
> by_region_filter<- with(majorSalesData, table(SalesTerritoryRegion, ProductKey))
> results_filters <- CA(by_region_trimmed, graph = FALSE)
[color=#f00]Error in is.table(X) : object 'by_region_trimmed' not found[/color]
>

I look forward to more of these tutorial.

Thanks,
Yogs


Hi Yogs, thanks for finding these Smile

I think that 'product_contributions' should be replaced by 'strength'. And 'by_region_trimmed' should be 'by_region_filter'.

Cheers,
Nick
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