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

sql query for counting the records of particular id and show in column

I have a following table:-

declare @tab table(name varchar(10),id int)

insert into @tab values ('A',1),('B',1),('C',1),('D',1),('E',2),('F',2)

I need following output:-

declare @tab1 table(name varchar(10),id int, cnt int)

insert into @tab1 values ('A',1,4),('B',1,4),('C',1,4),('D',1,4),('E',2,2),('F',2,2)
select * from @tab1

I tried following query:-

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

select name,id,count(*) as cnt
from @tab 
group by name,id

Thanks

>Solution :

Try this

select name
     , id
     , count(*) over(partition by id) as cnt
from @tab
;
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