简体   繁体   中英

Using DISTINCT and COUNT together

I have two different product_id with multiple keywords per each. 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.

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

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