You can use an indexed view that SQL Server will automatically support:
create table dbo.users (
ID int not null,
Activated bit not null
)
go
create view dbo.user_status_stats (Activated,user_count)
with schemabinding
as
select Activated,COUNT_BIG(*) from dbo.users group by Activated
go
create unique clustered index IX_user_status_stats on dbo.user_status_stats (Activated)
go
These are just two possible states, but can expand to more use using a different data type. As I said, in this case, SQL Server will support backstage calculations, so you can simply request a view:
SELECT user_count from user_status_stats with (NOEXPAND) where Activated = 1
and he won’t have to query the base table. You need to use the tooltip WITH (NOEXPAND)for the editions below (Enterprise / Developer).
, @Jim, COUNT (*) , () , , .