Solved

SQL count and and SUM

Posted on 2010-09-17
3
531 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle forms question 22 60
Oracle cursor lifecycle inside procedure. 2 37
Oracle performance tuning 2 53
Age Calculation from specific date 19 55
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
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…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

737 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