简体   繁体   中英

self join on multiple count columns

I need to write a query to and am having a hard time coming up with a result.

Here are my columns

EMAIL ITEM PRODUCT_CATEGORY

I need to select where a particular emailAddress has browsed a category at least 2 times and having at least 2 distinct products

so records might look like so

Email       ITEM     CATEGORY
joe@gmail   Bread    FOOD
joe@gmail   Banana   FOOD
joe@gmail   Grapes   FOOD
joe@gmail   Hammer   Tools
joe@gmail   Hammer   Tools
joe@gmail   File     Tools
meg@gmail   Grass    GARDEN
meg@gmail   Grass    GARDEN
meg@gmail   Grass    GARDEN
meg@gmail   Grass    GARDEN

And I would want the results to look like below. Meg is missing because her Items are all the same

Email           Categroy    DistinctItemCount
joe@gmail.com   Food        3  
joe@gmail.com   Tools       2

I have the Category covered but I cannot see how to add in the Item count requirement

select * from
(
  select Email ,Product_Category, count(Product_Category) As CatCount
  from Browsed 
  group by Email, Product_Category
)     a 
where CatCount >1

I have been looking at this for to long.

Thank you in advance if you can help out.

Try this (should work consistently across major RDBMSes)

SELECT Email, Product_Category Category, COUNT(DISTINCT Item) DistinctItemCount
  FROM Browsed 
 GROUP BY Email, Product_Category
HAVING COUNT(DISTINCT Item) > 1 

Output:

|     EMAIL | CATEGORY | DISTINCTITEMCOUNT |
--------------------------------------------
| joe@gmail |     FOOD |                 3 |
| joe@gmail |    Tools |                 2 |

Here is SQLFiddle demo (MySql)
Here is SQLFiddle demo (SQL Server)

In the future please specify RDBMS and its version when posting a SQL query question

Let's start by eliminating all duplicate entries in your input table. http://sqlfiddle.com/#!2/51991/3/0

SELECT DISTINCT Email, ITEM, CATEGORY 
 FROM Browsed

Next, let's get the counts of products by category where there are two or more products in each category. We use that first query inside this one. http://sqlfiddle.com/#!2/51991/7/0 This is a list of users and categories where the user looked up two or more different products in each category.

SELECT Email, Category, COUNT(*) Prodcount
  FROM (

         SELECT DISTINCT Email, ITEM, CATEGORY 
           FROM Browsed
        ) U 
  GROUP BY Email, Category
  HAVING Prodcount >= 2

Next we want to know users that looked up two or more distinct categories. That works like this: http://sqlfiddle.com/#!2/51991/8/0

SELECT Email, COUNT(*) Catcount
  FROM (

         SELECT DISTINCT Email, CATEGORY 
           FROM Browsed
        ) V
  GROUP BY Email
 HAVING Catcount >= 2

Cool. Now we know what users are in the running. It's the users whose names appear in BOTH these resultsets ... two or more categories, two or more items in each category. http://sqlfiddle.com/#!2/8673a/1/0

SELECT W.Email, W.Category, W.Prodcount 
  FROM (
  SELECT Email, Category, COUNT(*) Prodcount
        FROM (

               SELECT DISTINCT Email, ITEM, CATEGORY 
                 FROM Browsed
              ) U 
      GROUP BY Email, Category
      HAVING Prodcount >= 2
   ) W
   WHERE W.Email IN
   (
     SELECT Email 
       FROM (
             SELECT DISTINCT Email, CATEGORY 
               FROM Browsed
            ) V 
      GROUP BY Email
     HAVING COUNT(*) >= 2
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM