I need to run training queries that return results for questions such as: "Who completed this training, but not this training?"
In the simplified table below, I would like to know which employee completed training_id 1 (as indicated in the date in the complete_date field), but did not finish training_id 7.
+-------------+-------------+----------------+
| emp_id | training_id | completed_date |
+-------------+-------------+----------------+
| 1 | 1 | 2010-04-02 |
+-------------+-------------+----------------+
| 1 | 7 | Null |
+-------------+-------------+----------------+
| 2 | 1 | Null |
+-------------+-------------+----------------+
| 2 | 7 | Null |
+-------------+-------------+----------------+
The desired result would be emp_id 1, and we would like to return its completed training and incomplete training on query parameters:
+-------------+-------------+----------------+
| emp_id | training_id | completed_date |
+-------------+-------------+----------------+
| 1 | 1 | 2010-04-02 |
+-------------+-------------+----------------+
| 1 | 7 | Null |
+-------------+-------------+----------------+
I can't figure out how to do this with a regular request, because it seems like IF logic is needed. Example: Return the rows where this training is completed, and return the rows where this second training will not be complete, BUT ONLY if the first training is completed.
How can I express something like this in SQL?