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)
 finding row count of distinct entries in a table

Author  Topic 

vinay.a
Starting Member

20 Posts

Posted - 2008-08-12 : 02:56:09
hi,
I have a table of the following form:

EmpName---Department
--------------------
aaa-------Sales
bbb-------Finance
ccc-------Finance
ddd-------Production
eee-------Sales
fff--------Finance
ggg-------Production
hhh-------Finance
---------------------

I want to display the department and the count of employees of each department as follows:

Department----EmpCount
----------------------
Finance--------2
Production-----2
Sales----------4
----------------------
I refered some articles on inner join, etc. but did not find luck
Any help will be very useful. Thank you.

regards,
vinay

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-08-12 : 03:04:37
i'm gonna assume you got the counts in your example the wrong way round for sales and finance??

use count(distinct empname)
as in...

select department, count(distinct empname)
from YourTable
group by department

Em
Go to Top of Page

vinay.a
Starting Member

20 Posts

Posted - 2008-08-12 : 06:19:31
thanks, its working fine,

regards,
vinay
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-12 : 06:22:50
Why the distinct on empname ?

This will also do the job
select department, count(*)
from YourTable
group by department



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-12 : 08:14:23
quote:
Originally posted by khtan

Why the distinct on empname ?

This will also do the job
select department, count(*)
from YourTable
group by department



KH
[spoiler]Time is always against us[/spoiler]




may be influenced by title of the thread
Go to Top of Page
   

- Advertisement -