简体   繁体   中英

query to find the records between two tables for a given date range

I have a employee table which holds the information about which department the employee belongs during a period of time. At any point in a time a employee can belong to only one department. The end date column holds till what date the employee had stayed in a particular department. if the end date column holds a future date which means thats the latest department for a employee.

empid deptname startdate enddate
1 sales jan-20-2022 jan-24-2022
1 marketing jan-25-2022 feb-03-2022
1 support feb-04-2022 feb-06-2022
1 training feb-07-2022 dec-31-2050

I have a call details table which holds the information of which employee took the call and what is call start time and call end time.

call_id empid callstart_time callendtime
10 1 jan-21-2022 10:00:00 jan-21-2022 10:30:00
11 1 jan-21-2022 10:40:00 jan-21-2022 10:45:00
12 1 feb-01-2022 11:20:00 feb-01-2022 11:30:00
13 1 feb-05-2022 09:00:00 feb-05-2022 10:00:00
14 1 feb-08-2022 10:00:00 feb-08-2022 11:00:00

Now my question is:

I am looking for inputs and the sample query where i need to know what was the employees department during the time the employee took the call.

For example, if I want to know what are the calls took by an employee from jan-20-2022 to feb-02-2022 and what was there department name during the time of the call. i need the below output.

call_id empid callstart_time callendtime deptname
10 1 jan-21-2022 10:00:00 jan-21-2022 10:30:00 sales
11 1 jan-21-2022 10:40:00 jan-21-2022 10:45:00 sales
12 1 feb-01-2022 11:20:00 feb-01-2022 11:30:00 marketing

If i run the query for a date range from feb-04-2022 to feb-10-2022, i want to see the below output

call_id empid callstart_time callendtime deptname
13 1 feb-05-2022 09:00:00 feb-05-2022 10:00:00 support
14 1 feb-08-2022 10:00:00 feb-08-2022 11:00:00 training

please share few inputs on how to achieve this output using the sql query

A CROSS APPLY lets you define a subselect to pick the applicable employee record. In your case, the latest employee record prior to the call. Something like:

SELECT C.*, E.deptname
FROM calldetails C
CROSS APPLY (
    SELECT TOP 1 *
    FROM employee E
    WHERE E.empid = C.empid
    AND E.startdate <= C.callstart_time
    ORDER BY E.startdate DESC
) E
ORDER BY C.callstart_time

See this db<>fiddle

Or since you have end date, a simple join will do

SELECT C.*, E.deptname
FROM calldetails C
JOIN employee E
    ON E.empid = C.empid
    AND E.startdate <= C.callstart_time
    AND E.enddate > DATEADD(day, -1, C.callstart_time)
ORDER BY C.callstart_time

Note the date adjustment needed for the enddate comparison. This is needed because you are using inclusive enddates, Using exclusive end dates (where enddate = startdate for the next record works much better for range checks and calculations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM