Received: from marsh.owlnet.rice.edu (marsh-49.owlnet.rice.edu [128.42.49.222]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f7BB4CP47646 for ; Sat, 11 Aug 2001 07:04:12 -0400 (EDT) (envelope-from rainman@owlnet.rice.edu) Received: from owlnet.rice.edu (stud-assoc-pc2.rice.edu [128.42.185.75]) by marsh.owlnet.rice.edu (8.11.0/8.9.0) with ESMTP id f7BB49f09760 for ; Sat, 11 Aug 2001 06:04:09 -0500 (CDT) Message-ID: <3B751129.7030101@owlnet.rice.edu> Date: Sat, 11 Aug 2001 06:04:09 -0500 From: Joshua Adam Ginsberg Organization: Wiess College, Rice University User-Agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:0.9.2+) Gecko/20010729 X-Accept-Language: en-us MIME-Version: 1.0 To: pgsql-general@postgresql.org Subject: Query help... Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200108/281 X-Sequence-Number: 13524 A simplified version of my situation... I've got three tables that I'm working with... the first is a table of staff members... firstname, lastname, staffid... nothing tough... the second is a table of jobs... job title, jobid... nothing tough... the third is a history of job assignments... it's got a staffid, a jobid, a timestamp, and a field to denote whether the job was added or dropped... this third table exists because it is useful in this project to be able to look at a staff member's job history... The problem I'm running into is queries that attempt to determine who is an "active" member of the staff, defined as a staff member who currently has at least one job added but not dropped... this can be easily measured by saying that if the number of times a staff member had a job added is greater than the number of times that same staff member had a job dropped, that staff member is active... What I'm having trouble figuring out is how can I incorporate those rowcounts into a greater query... I've succeeded in some other cases... I can get a list of the active personnel: select staff.staff_id, firstnames, lastname, jobs.job_id, job_title from staff, staff_history, jobs where staff.staff_id = staff+history.staff_id and jobs.job_id = staff_history.job_id group by staff.staff_id, firstnames, lastname, jobs.job_id, job_title having (count(add_or_drop) % 2) = 1 order by lastname, firstnames; This succeeds because for each person-job combination, if there's 0 mod 2 instances of them, then the person has added and dropped the job (some number of times)... if there's 1 mod 2 instances, then the person has added but not dropped the job... I can get a list of jobs and who is actively assigned to the job: select jobs.job_id, job_title, staff.staff_id, firstnames, lastname from jobs left outer join (staff_history join staff on (staff_history.staff_id = staff.staff_id)) on (jobs.job_id = staff_history.job_id) group by jobs.job_id, staff.staff_id, job_title, firstnames, lastname having (count(staff.staff_id) % 2 = 1); This succeeds because for each grouping, which again is a person-job combination, I can eliminate anybody who has been assigned and deassigned to the same job the same number of times... But here's a case where I can't figure it out... I'm trying to generate a list of staff members with the "active" staff members first... now, I can't use my same of mod 2 here, because a person can have more than one job... so if i have been added to two jobs but not removed from any, count(add_or_drop) % 2 = 0, but I am active... Any suggestions? -jag -- -------------------------------------------------------- Joshua Ginsberg rainman@owlnet.rice.edu Director of Technology dirtech@sa.rice.edu Student Association AIM: L0stInTheDesert Rice University, Houston, TX ======================================================== "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." -Rich Cook --------------------------------------------------------