0

Simple question here but I don't have any data to test with so I'm not sure if it is right.

Let's say I have a table that looks something like this:

+---------+---------+
| ColumnA | ColumnB |
+---------+---------+
|       1 | a       |
|       2 | a       |
|       3 | a       |
|       3 | b       |
|       4 | a       |
|       5 | a       |
|       6 | a       |
|       6 | b       |
|       6 | c       |
|       7 | a       |
+---------+---------+

I want to count the number of distinct items in column b for each column a.

So the result table would look like this:

+---------+---------+
| ColumnA | Count   |
+---------+---------+
|       1 |       1 |
|       2 |       1 |
|       3 |       2 |
|       4 |       1 |
|       5 |       1 |
|       6 |       3 |
|       7 |       1 |
+---------+---------+

How would I do this?

I've been trying something like this:

dense_rank() over (partition by ColumnA order by ColumnB) count

1 Answer 1

1

You can use GROUP BY and COUNT with DISTINCT:

SELECT ColumnA, COUNT(DISTINCT ColumnB) AS [Count]
FROM MyTable 
GROUP BY ColumnA

demo on dbfiddle.uk

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.