Solved

SQL count and and SUM

Posted on 2010-09-17
3
529 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

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

Suggested Solutions

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

830 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