Note that there are some explanatory texts on larger screens.

plurals
  1. POConditional Query for Employee Training Analysis Query
    text
    copied!<p>I need to run training queries that return results for questions like the following: "Who has completed this training, but not that training?"</p> <p>In the simplified table below, I would like to know which employee has completed training_id 1 (as indicated by a date in the completed_date field), but has not finished training_id 7.</p> <pre><code>+-------------+-------------+----------------+ | emp_id | training_id | completed_date | +-------------+-------------+----------------+ | 1 | 1 | 2010-04-02 | +-------------+-------------+----------------+ | 1 | 7 | Null | +-------------+-------------+----------------+ | 2 | 1 | Null | +-------------+-------------+----------------+ | 2 | 7 | Null | +-------------+-------------+----------------+ </code></pre> <p>Desired result would be emp_id 1 and we'd like to return his/her completed training and non-completed training per the query parameters:</p> <pre><code>+-------------+-------------+----------------+ | emp_id | training_id | completed_date | +-------------+-------------+----------------+ | 1 | 1 | 2010-04-02 | +-------------+-------------+----------------+ | 1 | 7 | Null | +-------------+-------------+----------------+ </code></pre> <p>I can't figure out how to do this with a regular query because it seems to need IF logic. Ex: Return rows where this one training is complete and return rows where this second training is not complete BUT ONLY if the first training is complete.</p> <p>How do I express something like that in SQL?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload