from this table, I want to return the worker who has served the longest. However, anyone with the 'Leader' position is excluded from being the longest serving.
From this result, I want to get the Worker_ID of the leader(s)
SAMPLE DATA
CREATE TABLE `Worker_Department` (
`Worker_ID` Integer NOT NULL ,
`Department_ID` Integer NOT NULL,
`Position` Text NOT NULL,
`Start_Date` datetime NOT NULL,
`Leave_Date` datetime
);
INSERT INTO `Worker_Department`(`Worker_ID`,`Department_ID`,`Position`,`Start_Date`)
VALUES
(10,100,'Leader','1980-11-11'),
(20,200,'Leader','1980-11-11');
INSERT INTO
`Worker_Department`(`Worker_ID`,`Department_ID`,`Position`,`Start_Date`,`Leave_Date`)
VALUES
(30,200,'Administrator','1980-11-11', '2014-02-02'),
(40,200,'Receptionist','1975-11-11', '2014-02-02');
INSERT INTO `Worker_Department`(`Worker_ID`,`Department_ID`,`Position`,`Start_Date`)
VALUES
(50,300,'Administrator','2014-02-02'),
(30,100,'Administrator','2014-02-02');
From the provided test data, it is expected that worker 30 is the longest serving worker. This should result in worker 10 alongside any other leaders at department 100 being outputted.
Currently, I have a query that will return the current date subtract the minimum start date for each worker, except those that are a leader. This tells me how long that worker has been with the company.
SELECT Worker_ID, Now() - Min(start_date) as NowSubMin FROM Worker_Department
WHERE position != 'Leader'
GROUP BY Worker_ID
I also have a query that will display the current workers except for the leaders
SELECT Department_ID FROM Worker_Department
WHERE position != 'Leader' AND leave_date is null
However, I am unsure as to how to write a query that combines the information gathered from the previous two queries to get the department leader.