I was reworking the list of SQL Scripts for DynamicAccounting.net and I realized that it would be nice to have some SQL code that showed related items. In other word, something similar to Amazon.com’s “Customers who bought “X” also bought “A”, “M” and “Y”.
I wanted to add a frequency to make it more relevant. What I was looking for was “Customers who bought “X” also bought “A” 56% of the time, “M” 42% of the time and “Y” 38% of the time. I could swear I’ve seen code like this but I couldn’t find it, so…I created it.
I built a SQL Stored Procedure that accepts an inventory item as it’s only parameter and returns other items that were sold on the same invoices along with the percentage of how often they appeared together.
For example, executing the procedure with the item “5-Fee” looks like this: exec BoughtRelated '5-Fee' and returns this:
This won’t work in SmartList Builder since SLB won’t use a Stored Procedure. To work around that I’ve included a download that does the same thing with Views instead of a stored proc. It’s not as efficient and it takes 3 views to make it happen though I suspect someone smarter than me can make it cleaner. The only other advantages (maybe) to the views is that you could return results for all items, not just one. This could be a plus or it could severely bog down your system if you have enough items and invoices. I told you it was inefficient.
Finally this works off of SOP invoices under the theory that it’s not really bought until it’s invoiced. Also, I didn’t bother including returns because it’s not really relevant from a sales/pairing standpoint. The returns piece is really a different question.
I’ll add these to the permanent SQL Scripts as well.
The files are here:
Related Items Procedure
Related Items Views