Home > OS >  SQL Count number of occurrences for each distinct combination
SQL Count number of occurrences for each distinct combination

Time:10-22

I need to get the count for each distinct combination of two columns. Here is the query to get the distinct combinations.

SELECT distinct Sales_Cat, Sub_cat
FROM rtx_Sales

It returns all combinations as seen below. I need to add a count field that also shows the number of occurrences for each of the combinations. Is SQL capable of this or should I write a python script to query for each combo?

enter image description here

CodePudding user response:

Use COUNT()

SELECT sales_cat, sub_cat, COUNT(*)
FROM rtx_sales 
GROUP BY sales_cat, sub_cat
  • Related