Solved

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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 …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

809 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