Skip to content
Advertisement

Distincted ids for grouped values

I want to count the distinct ids in each numb and store them in a column : Tried this:

Receive this output:

As you can see COUNTER columns has the count of distincted ids but for all NUMB

I want to output this:

  1. 1515 has 2 ids
  2. 1921 has 1 id
  3. 2121 has 2 ids

I tried also to place a GROUP BY NUMB inside (SELECT COUNT(DISTINCT ID) FROM dbo.test tp WHERE ORIG= '4567') but didn’t work.

Advertisement

Answer

What you seem to want is:

Alas, SQL Server doesn’t support count(distinct) with window functions.

Happily, there is an easy workaround (which begs the question as to why the above syntax is not supported):

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement