Solved

SQL - How to write a query retrieve number of tasks in employee table

Posted on 2013-05-13
2
345 Views
Last Modified: 2013-05-13
Hi,

I have an task table that has the following fields,  where is has the name of the task and the employee that is working on that task

TaskName            ID                  Department                 EmpID
Task a                   1234                     A                               2
Task b                   1235                     A                               2
Task c                   1236                      A                              2
Task a                   1234                     A                               5
Task c                   1236                      A                              5

I am looking for a way to get the total number of tasks that each employee has per department, so the result should look like this :

Name           No. of Tasks
Person a               3
Person b               2

Any ideas on how to do this?
0
Comment
Question by:Jasmin01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 39161405
something like this:

select
  e.person_name
, count(distinct t.id) as no_of_tasks
from task_table as t
inner join emp_table as e on t.empid = e.id
group by
  e.person_name
0
 
LVL 1

Accepted Solution

by:
muh_abbas earned 250 total points
ID: 39161469
Hi Jasmin01,


You can achive this simple using group by clause, query will be as follow;


select TaskName,count(TaskName) as 'No. of Tasks'
from Tasks
group by TaskName


If you want to filter over department also, you need to add the department in group by clause also.


select TaskName,count(TaskName) as 'No. of Tasks'
from Tasks
group by TaskName,Department
having Department='A'


Regards,
Abbas
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question