Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting count of each item

Tags:

sql

I have the following query

Select count(*) as TotalCount from ABC where ID in (1,3,6)

The problem here is that it gives me the total count of 1,3 and 6. But I would like to get the count for each ID, something like shown below:

TotalCount, ID
6,          1
2,          3
5,          6 

Kindly let me know the SQL Server query for achieving this. I wouldn't prefer to use Temp tables.

like image 928
Derin Avatar asked Oct 18 '25 01:10

Derin


1 Answers

simply add the id to the selected columns and group by them:

Select id, count(*) as TotalCount from ABC where ID in (1,3,6)
group by id
like image 117
krock Avatar answered Oct 19 '25 21:10

krock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!