Friday, 17 October 2014

How to use multiple aggregate functions in a single query(without sub query) in oracle/sql server


Here i am using two count functions with different conditions in a single query.


select count(case when department_id = 10 and salary<5000 then 1 end), 
          count(case when department_id = 20 then 1 end) 
from employees;

Output:





If we want the output as the two results in same column,


select count(EMPLOYEE_ID) from employees where DEPARTMENT_ID=30 and salary<5000
union all

select count(EMPLOYEE_ID) from EMPLOYEES where DEPARTMENT_ID=50;

Output:








References: http://stackoverflow.com/questions/12789396/how-to-get-multiple-counts-with-one-sql-query.


1 comment: