Solved

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

Posted on 2013-05-13
2
332 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
2 Comments
 
LVL 48

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

733 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