Skip to content
Advertisement

SSMS SQL Identify purchases who only buy one specific item

I want to find customers who have only bought fruit in their purchase. My data looks like this:

I want this:

Thinking I need a where clause, grouped by ID and Purchase_date?

Advertisement

Answer

You can try to use Having with condition aggregate function to write the logic.

  • Buy fruit (COUNT with fruit count will be greater than 1 )
  • Din’t buy any product without Fruit

Query 1:

Results:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement