sql server - Optimizing a dashboard query -


i'm building application , in app want show simple statistics number of items in system , in various states , i've created query while works fell though incredibly inefficient, honest i'm not sure how go making better.

what have ended cute nothing more bunch of sub selects in order dashboard stats such:

with cte_dashboard(active_employees, inactive_employees, linked_employees, total_employees,                  ongoing, completed, pending, cancelled, total_contracts) (     select          (select count(id) [tam].[employees] isactive = 1) 'active',         (select count(id) [tam].[employees] isactive = 0) 'inactive',         (select count(id) [tam].[employees] userid not null) 'linked',         (select count(id) [tam].[employees]) 'total',         (select count(id) [tam].[contracts] status = 'ongoing') 'ongoing',         (select count(id) [tam].[contracts] status = 'completed') 'completed',         (select count(id) [tam].[contracts] status = 'pending') 'pending',         (select count(id) [tam].[contracts] status = 'cancelled') 'cacnelled',         (select count(id) [tam].[contracts]) 'total' )      select * cte_dashboard; 

how can make better without of these subqueries, or there do?

this might work :

select * (     select          sum(case when isactive = 1 1 else 0 end) [active],         sum(case when isactive = 0 1 else 0 end) [inactive],         sum(case when userid not null 1 else 0 end) [linked],         count(*) [total] -- or sum(1)     [tam].[employees] ) cross join (     select          sum(case when status = 'ongoing' 1 else 0 end) [ongoing],         sum(case when status = 'completed' 1 else 0 end) [completed],         sum(case when status = 'pending' 1 else 0 end) [pending],         sum(case when status = 'cancelled' 1 else 0 end) [cancelled],         count(*) [total] -- or sum(1)     [tam].[contracts] ) b 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -