I need select the employee id's from the last delivery unless the status = OTD from the hta table. Then I need to get all remaining employee id's from the employee table and display those results. Unfortunately employees with a status of otd are also in the employee table. Can I write a query that will return these results? Or do I have to do it differently? If so can you guide me.
SELECT h.employee_id
, MAX(h.delivery_date)
, e.employee_first
, h.status
FROM TABLE_HTA h
RIGHT
JOIN TABLE_EMPLOYEES e
ON h.employee_id = e.employee_id
WHERE h.status <> "OTD"
GROUP
BY h.employee_id
ORDER
BY MAX(h.delivery_date) ASC
Table_hta<br>
order_id | employee_id | status | delivery_date |<br>
1 | 23 | OTD | 1/1/15 3:45pm<br>
2 | 24 |DELIVER | 1/1/15 3:50pm<br>
3 | 25 |DELIVER | 1/1/15 3:51pm<br>
<br>
Table_employees<br>
employee_id | employee_first |<br>
23 Bob<br>
24 James<br>
25 Henry<br>
26 Sally<br>
Results<br>
employee_id | employee_first | delivery_date<br>
26 |Sally<br>
24 |James |1/1/15 3:50pm<br>
25 |James |1/1/15 3:51pm<br>
Sally was included because she did not have the status OTD. Bob was excluded because he had the status OTD.