pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joshua Adam Ginsberg <rainman@owlnet.rice.edu>
To: pgsql-general@postgresql.org
Subject: Query help...
Date: Sat, 11 Aug 2001 06:04:09 -0500
Message-ID: <3B751129.7030101@owlnet.rice.edu> (raw)

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
--------------------------------------------------------




view thread (70+ messages)  latest in thread

Message-ID: <3B751129.7030101@owlnet.rice.edu>
Permalink:  ../3B751129.7030101@owlnet.rice.edu/
Also on:    postgresql.org/message-id/3B751129.7030101@owlnet.rice.edu

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: rainman@owlnet.rice.edu
  Subject: Re: Query help...
  In-Reply-To: <3B751129.7030101@owlnet.rice.edu>
  References: <3B751129.7030101@owlnet.rice.edu>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox