[英]Get a distinct row count from another column
SQL Table is as follows: SQL 表如下:
Category | Subcategory |
A 1
A 1
A 2
B 1
B 2
I need the number of each subcategory for each category, not including duplicate subcategories within the category.我需要每个类别的每个子类别的数量,不包括类别中重复的子类别。
You'll notice there are 3 total "1" subcategories, but only a count of 2 as the duplicate is redundant and not included.您会注意到总共有 3 个“1”子类别,但只有 2 个,因为重复项是多余的且未包含在内。
Example output:示例 output:
subcategory | count
1 2
2 2
How can I achieve this?我怎样才能做到这一点? I am familiar with COUNT but I can only get the raw number of rows.
我熟悉 COUNT 但我只能获得原始行数。
Using Snowflake.使用雪花。
Thanks!谢谢!
You can use GROUP BY
, as in:您可以使用
GROUP BY
,如:
select Category, count(distinct Subcategory)
from t
group by Category
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.