Solved

SQL count and and SUM

Posted on 2010-09-17
3
518 Views
Last Modified: 2013-12-07
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
Comment
Question by:gosvald
3 Comments
 

Accepted Solution

by:
kannanbabu earned 125 total points
ID: 33699720
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
 
LVL 1

Assisted Solution

by:sudheerg
sudheerg earned 125 total points
ID: 33699758
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
 
LVL 2

Author Comment

by:gosvald
ID: 33699910
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

757 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