All pastes #394285 Raw Edit

Untitled

public sql v1 · immutable
·published 2007-03-14 05:17 UTC
rendered paste body
/*I have the two tables listed below and want to create query that reutrns the employee's id, every request he's turned in, and how many people have that same request in and were hired before him.  For example if employee01 was request 10am and 12pm shifts and he was first one the list for 10am and 4th (3 above him) on the list for 12pm the results would be:"Employee01", "10am", "0""Employee01", "12pm", "3"-----         | -----Employees     | ShiftRequests-----         | -----id - pk       | id - pkHired - Date  | empID - fk              | request - VarcharThis query works but I'm wondering if it can be done in a better way:*/SELECT * FROM (ShiftRequests AS r INNER JOIN Employees AS e ON r.empID = e.id)INNER JOIN(SELECT request, count(1) as [Position] FROM ShiftRequests AS srINNER JOIN Employees AS em ON sr.empID = em.id WHERE hired < (SELECT hired FROM Employees WHERE id = 'emp01') GROUP BY request) AS c ON c.request = r.requestWHERE e.id = 'emp01';