Scenario:
Find out any customers in each factory that have three or more orders which contain new items they have never purchased before. For example, John has:
Order A: Item 1, Item 2
Order B: Item 2, Item 3, Item 4
Order C: Item 3
Order B has items John has never purchased before, while Order C does not.
To put it another way: Starting with each customer's second order, we want to know how many orders they have with at least one item that's never been on one of their orders before.
Table schema: see SQL Fiddle. RelatedRecordID binds multiple records into a single order.
My approach: see also SQL Fiddle above. This approach is flawed and not robust. In the previous John example, this method will give out 2 increments, but because Order C is placed after B, there is actually only one increment. And if Order B does not contain Item 3, its precedence won't affect the result.
Can anybody think of a fix or a new way to do this?
Best Answer
The CTE first finds the first order than every item was purchased (per customer and factory) and then we simply count the distinct order IDs (and subtract 1 as the very first order doesn't count):
Tested in SQLfiddle.com