I need some help with a question. I work in SQL SERVER 2012. Basically this is the case:
A customer can buy three kinds of products:
- A
- B
- C
If a customer buys two products( any combination, exemple, A+B OR B+C OR CA, any combination that results in two sold products) on the same day, we should show this as "Dual".
If a customer only buys 1 product on the same day, then we should call it "Single".
If the customer buys All three of the products on the same day, we should call it "Triple".
This is what my data looks like at the moment:
YearMonth Product CustomerNr Sales Date
201505 B 70056844 20150501
201505 A 70056844 20150501
201505 B 70057297 20150503
201505 A 70057494 20150504
201505 B 70057494 20150504
201505 C 70057494 20150504
201505 B 70033055 20150506
201505 B 36021632 20150508
201505 A 70060612 20150508
201505 C 70060612 20150508
As we can see in the sample data, customer nr :70060612 has bought two products A and C on the same sales date and hence we can say he bought a dual package. Customer number :70057494 has bought on the same sales date, 3 products, A,B and C. We can call it Triple.
I want my data to include a column like this :
YearMonth Product CustomerNr Sales Date Package
201505 B 70056844 20150501 Dual
201505 A 70056844 20150501 Dual
201505 B 70057297 20150503 Single
201505 A 70057494 20150504 Triple
201505 B 70057494 20150504 Triple
201505 C 70057494 20150504 Triple
201505 B 70033055 20150506 Single
201505 B 36021632 20150508 Single
201505 A 70060612 20150508 Dual
201505 C 70060612 20150508 Dual
How can I do this? I was thinking of doing some like
Test=COUNT(*) OVER (PARTITION BY CustomerNr)
This is a solution with an corresponding data example. Hopefully this helps you.
Best regards, Ionic