Solved

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

Posted on 2013-05-13
2
297 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now