请尝试以下...
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;
该语句使用以下子查询来形成的user
,file_download
和product_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
上述子查询的结果显示在下面的子查询中使用,以获得该user
已经从网上下载file
多少product_id
值的个性化......
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
产生的数据集然后在product_id
值的的user
和file_download
每个组合的计数被有效地追加到每对应的记录在customer_statistics
这样的方式连接到一个实例customer_statistics
。
从该接合产生的数据集,然后通过的user
,file_download
和product_id
每个唯一组合以及属于各组(记录的计数分组即,每个时间的计数,一个user
已经下载一个特定file
从product_id
)被计算。
我不记得是否MySQL
要求CountOfProducts
被GROUP BY
使用。但是,尽管user
,file_download
和product_id
的每个组合都决定了CountOfProducts
的值,但许多形式的SQL
都要求您选择每个非聚合字段的GROUP BY
。因此,自从将CountOfProducts
到GROUP BY
没有任何伤害,我已经包括了GROUP BY
子句中CountOfProducts
。
如果一个或两个以上规则可以澄清关于它们的结构,则所显示的句子可以被自动生成。
如果您有任何问题或意见,请随时发布相应评论。
附录
要排除从结果集的单个用户,请使用以下的变化。
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
值未出现在父子查询的结果中,因此当主语句的INNER JOIN
部分基于User
的共享值执行时,目标User
也将从连接数据集中排除。
通过添加WHERE
子句到最里面的子查询,我避免不必要的处理少量由语句的中间和外水平,从而使得整体语句略微比如果user
值被排除在更高效的中层或外层。
同样,如果需要排除多于一个User
,您可以通过将它们的值User
显式编码到语句中或通过连接到排除值表来排除它们。对于第一种情况使用...
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 NOT IN ('Sam', 'I', 'Am')
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;
在第二种情况下使用...
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 NOT IN (SELECT user
FROM excludedUsers
)
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;
你想要的结果看起来像*那*? – Strawberry
@Strawberry - 当然不是,我只是想要这些价值观 - 我这样拼写出来,所以很容易理解。 –
那么,你能拼出来吗? – Strawberry