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
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1274 Visits: 303
Comments posted to this topic are about the item Analysing Sales Patterns: R + SQL Server
Kyrilluk
Kyrilluk
Right there with Babe
Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)

Group: General Forum Members
Points: 760 Visits: 394
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
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1274 Visits: 303
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
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 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
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 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-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38726 Visits: 886
Thanks for the great article.
bteague
bteague
SSC-Addicted
SSC-Addicted (469 reputation)SSC-Addicted (469 reputation)SSC-Addicted (469 reputation)SSC-Addicted (469 reputation)SSC-Addicted (469 reputation)SSC-Addicted (469 reputation)SSC-Addicted (469 reputation)SSC-Addicted (469 reputation)

Group: General Forum Members
Points: 469 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 Burstein
Alan Burstein
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32446 Visits: 8577
Great article!

-- Alan Burstein


Helpful links:

Best practices for getting help on SQLServerCentral -- Jeff Moden
How to Post Performance Problems -- Gail Shaw

Nasty fast set-based string manipulation functions:
For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)
To split strings based on patterns try PatternSplitCM
Need to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8K

I cant 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. -- Itzik Ben-Gan 2001

yogmangela
yogmangela
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 55
have you created ODBC connection form Data Source ?
nick.dale.burns
nick.dale.burns
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1274 Visits: 303
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