简体   繁体   English

在多个计数列上自行联接

[英]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 电子邮件项目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 我需要选择特定的emailAddress至少浏览过2次类别并且至少具有2种不同的产品的位置

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) 尝试此操作(应在主要的RDBMS上一致地工作)

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) 这是SQLFiddle演示(MySql)
Here is SQLFiddle demo (SQL Server) 这是SQLFiddle演示(SQL Server)

In the future please specify RDBMS and its version when posting a SQL query question 以后在发布SQL查询问题时,请指定RDBMS及其版本

Let's start by eliminating all duplicate entries in your input table. 让我们从消除输入表中所有重复的条目开始。 http://sqlfiddle.com/#!2/51991/3/0 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. http://sqlfiddle.com/#!2/51991/7/0这是用户和类别的列表,其中用户在每个类别中查找两种或更多种不同的产品。

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 像这样工作: 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 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
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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