Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

911 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

23 Experts available now in Live!

Get 1:1 Help Now