• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • Last Modified:

SQL count and and SUM

Hi There,

I am trying to work out this query in POSTGRESQL database.  I need to list all workers that have done more than 3 jobs. That part of query works fine. It lists all workers with more than 3 jobs in the 'job' table. The job table lists worker_id with every job that was done so I am simply counting the number of times worker_id occurs and then list only those that are listed more then 3 times. What I need now is to add a column that would give me a total number of jobs done by those workers as well.

So the result set would be something like:

John Smith 35
Bill Cosby 40
Dan White 4

SELECT person.first_name, person.surname
FROM person INNER JOIN job on (person.person_id = job.worker_id)
GROUP BY person.first_name, person.surname
HAVING COUNT(job.worker_id) > 3
0
gosvald
Asked:
gosvald
2 Solutions
 
kannanbabuCommented:
SELECT person.first_name, person.surname, count(job.worker_id) as Job_Count
FROM person INNER JOIN job on (person.person_id = job.worker_id)
GROUP BY person.first_name, person.surname
HAVING COUNT(job.worker_id) > 3
0
 
sudheergCommented:
Hi ,

Just add the column count(job.worker_id) in the select clause like so:

SELECT person.first_name, person.surname
FROM person INNER JOIN job on (person.person_id = job.worked_id)
GROUP BY person.first_name, person.surname
HAVING count(job.worked_id)>3

You'll get what u need.
0
 
gosvaldAuthor Commented:
Thanks guys. I actually had this already but I was getting errors. Then I realized that case was wrong (POSTGRESQL is case sensitive)!
Anyway. Thanks for the help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now