0

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

3
  • I think follow an SQL course, and find out what GROUP BY can do .... see: learn.microsoft.com/en-us/sql/t-sql/queries/… Commented Oct 20, 2022 at 19:21
  • 1
    select sales_cat, sub_cat, count('Dracula') as cnt from rtx_sales group by sales_cat, sub_cat Commented Oct 20, 2022 at 19:21
  • been a few years since I have touched sql so yes I do need a refresher lol - thank you though Commented Oct 20, 2022 at 19:23

1 Answer 1

1

Use COUNT()

SELECT sales_cat, sub_cat, COUNT(*)
FROM rtx_sales 
GROUP BY sales_cat, sub_cat
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.