How to Have Multiple Counts in SQL Server

To do multiple counts in one query in SQL Server, you can combine sum() with the CASE statement:

  count(1), -- count all products
  sum(case when name = 'Table' then 1 else 0 end), -- count all tables
  sum(case when category = 5 then 1 else 0 end) -- count all category 5 products
from products;
From SQL Server query to chart to Slack in seconds
Get to answers faster, together, with PopSQL and SQL Server