简体   繁体   English

一起使用DISTINCT和COUNT

[英]Using DISTINCT and COUNT together

I have two different product_id with multiple keywords per each. 我有两个不同的product_id,每个都有多个关键字。 Following statement is not working: 以下语句不起作用:

SELECT distinct product_id, COUNT(distinct keyword) as keyword_sum FROM data;

Currently result looks like this: 当前结果如下:

product_id     keyword_sum
2              47

Desired result would look like this: 所需的结果如下所示:

product_id     keyword_sum
2              26
3              21

Any help much appreciated! 任何帮助,不胜感激!

Group by the column you want to be unique and then you can use aggregate functions like count on each element of the group. 按希望唯一的列分组,然后可以对组中的每个元素使用聚合函数(如count

SELECT product_id, COUNT(distinct keyword) as keyword_sum 
FROM data
GROUP BY product_id

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

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