Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count number of rows for each id to create a new column

ID value
1 4
1 5
3 4
2 10

I want to add another column called count, that has for each id the number of observations.

Transformed table

id value count
1 4 2
1 5 2
3 4 1
2 10 1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the OVER() clause to aggregate.

SELECT 
  ID, 
  value, 
  [count] = COUNT(*) OVER (PARTITION BY ID)
FROM dbo.TableName;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading