如何获取子DISTINCT列的DISTINCT列和COUNT次出现

时间:2017-05-07 23:03:54

标签: mysql sql

我在构建文件下载统计数据库并显示我想要的信息方面遇到了一些困难。

表:customer_statistics

3

我需要根据上表显示以下内容:

  

Tom已从file_3.pdf个不同的产品下载product_id 1048,但已从file_1.pdf下载1 4次。

     

Tom还从product_id产品下载了7,只从4下载了一次

     

Tom从file_3.pdf个不同的产品下载2

     

Sue已从file_3.pdf个不同的产品下载product_id 1284,但已从file_1.pdf下载1 2次。

     

Sue还从product_id产品下载了file_2.pdf,只从1下载了一次

     

Sue还从product_id产品下载了6,只从5下载了一次

     

Sue从taskkill /F /IM adb.exe /T种不同的产品下载{{1}}

最好的方法是什么?

我需要重组我的桌子吗?

先谢谢!

1 个答案:

答案 0 :(得分:1)

请尝试以下方法......

SELECT user AS user,
       file_download AS file_download,
       product_id AS product_id,
       COUNT( * ) AS CountPerProduct,
       CountOfProducts AS CountOfProducts
FROM customer_statistics
JOIN ( SELECT user AS user,
              file_download AS file_download,
              COUNT( product_id ) AS CountOfProducts
       FROM ( SELECT user AS user,
                     file_download AS file_download,
                     product_id AS product_id
              FROM customer_statistics
              GROUP BY user,
                       file_download,
                       product_id
            ) AS uniqueComboFinder
       GROUP BY user,
                file_download
     ) AS CountOfProductsFinder ON customer_statistics.user = CountOfProductsFinder.user
                               AND customer_statistics.file_download = CountOfProductsFinder.file_download
GROUP BY user,
         file_download,
         product_id,
         CountOfProducts;

此语句首先使用以下子查询来形成userfile_downloadproduct_id的唯一组合列表...

SELECT user AS user,
       file_download AS file_download,
       product_id AS product_id
FROM customer_statistics
GROUP BY user,
         file_download,
         product_id

上述子查询的结果用于以下子查询中,以计算product_id已从{...}下载user的{​​{1}}个值的数量。

file

然后,将结果数据集连接到实例SELECT user AS user, file_download AS file_download, COUNT( product_id ) AS CountOfProducts FROM ( SELECT user AS user, file_download AS file_download, product_id AS product_id FROM customer_statistics GROUP BY user, file_download, product_id ) AS uniqueComboFinder GROUP BY user, file_download ,使得customer_statisticsproduct_id的每个组合的user值的计数有效地附加到file_download中的每个相应记录。

然后,根据customer_statisticsuserfile_download的每个唯一组合以及属于每个组的记录数(即每次的计数)对此加入产生的数据集进行分组已计算product_iduser下载了特定file}。

我无法记住product_id是否需要MySQL使用CountOfProducts。但是,即使GROUP BYuserfile_download的每个组合确定了product_id的价值,许多形式的CountOfProducts都要求您SQL 1}}选中每个非聚集字段。因此,由于将GROUP BY添加到CountOfProducts没有任何损害,因此我在GROUP BY子句中添加了CountOfProducts

如果可以澄清一两个关于其结构的规则,则可以自动生成显示的句子。

如果您有任何问题或意见,请随时发表评论。

附录

要从结果集中排除单个用户,请使用以下变体。

GROUP BY

我在这里使用了SELECT user AS user, file_download AS file_download, product_id AS product_id, COUNT( * ) AS CountPerProduct, CountOfProducts AS CountOfProducts FROM customer_statistics JOIN ( SELECT user AS user, file_download AS file_download, COUNT( product_id ) AS CountOfProducts FROM ( SELECT user AS user, file_download AS file_download, product_id AS product_id FROM customer_statistics WHERE user <> excludedUser GROUP BY user, file_download, product_id ) AS uniqueComboFinder GROUP BY user, file_download ) AS CountOfProductsFinder ON customer_statistics.user = CountOfProductsFinder.user AND customer_statistics.file_download = CountOfProductsFinder.file_download GROUP BY user, file_download, product_id, CountOfProducts; ,但您可以使用常量值(例如excludedUser)或保存目标值的变量替换它。

请注意,我已将Sam子句添加到最里面的子查询中。因为其父子查询的结果完全基于最里面的子查询的结果,所以排除的用户将不会在父子查询的结果中表示。并且由于排除的WHERE user <> excludedUser值未出现在父子查询的结果中,因此主要语句User基于(部分)对{{的共享值)执行1}},然后目标INNER JOIN也将从已连接的数据集中排除。

通过将User子句添加到最里面的子查询,我避免了语句的中间和外部级别的少量不必要的处理,从而使整个语句比{{1}更有效。 }值被排除在中间或外层。

同样,如果需要排除多个User,则可以通过将WHERE的值显式编码到语句中或通过连接到排除值表来排除它们。对于第一种情况使用......

user

在第二种情况下使用......

User