Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Group by Query...

Author  Topic 

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2005-08-06 : 06:01:28
Hi All,

I have the table Called Budget in which is there is column name called EmpID now if the EmpId -1 then its a vacant employee else its some occupied employee.
EmpId Name
1 XYZ
2 ABC
-1 Vacant
-1 Vacant
3 POR

I want the following output
Count Type
3 Occupied
2 Vacant


Thanks in Advance.

Complicated things can be done by simple thinking

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-06 : 06:16:27

Select sum(case when EmpId>0 then 1 else 0 end),'Occupied' from Budget
Union all
Select sum(case when EmpId<0 then 1 else 0 end),'Vacant' from Budget


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2005-08-06 : 06:27:45
Thanks madhivanan.. u r really helpful..

but i would like to know this query cannot be done by group by Clause..???

This worked perfectly for me.. Thanks once again

Complicated things can be done by simple thinking
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-06 : 06:37:06
Other possible method is

select count(Empid),'Occupied' from Budget where Empid>0
Union all
select count(EmpId),'Vacant' from Budget where Empid<0

Because you need extra column to specify Occupied or Vacant, it becomes complex if you do it in single query

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -