Arun February 2016

SQL Query to find exclusive customer

I have a table with two columns Cust and Category,

**Cust   Category**

   1         X
   2         Y
   1         Y
   2         Z
   3         X
   4         D

Customer 1 has purchased from category X and Y, but for 'Category X', 3 is an exclusive customer since Cust 3 purchased only from X. I want a result like below with three columns (Category, No of Customer purchased in the category and Exclusive Customer):

**Category  NoOfCust  ExclusiveCust**

    X         2              3
    Y         2             NULL
    Z         1             NULL
    D         1              4 

How can I query the table to get this result?

Answers


Tim Southard February 2016

Used a temp table to duplicate your structure:

SELECT 
    Category, 
    COUNT(*) as NoOfCust, 
    CASE WHEN COUNT(*) = 1 THEN MAX(ID_W) ELSE NULL END as Exclusive
FROM #WORKER T
GROUP BY Category

Post Status

Asked in February 2016
Viewed 1,035 times
Voted 4
Answered 1 times

Search




Leave an answer