简体   繁体   中英

SQL - Date range Return all data

I have a simple PHP web app for daily attendance of empployee I have a table named emp_master it would have all the employee data for example

emp_id   emp_name
1001     Abby
1002     Buub

And another table named timesheet_tran with dailt transaction when even employee log into the system

timesheet_tran table
date       emp_id 
02/01/14   1001
02/01/14   1002
03/01/14   1001
04/01/14   1001
04/01/14   1002

I was trying to figure out a single query to return data , between date range which would display all the employees who have logged / not logged for that day between the date range.

i need to display

emp_id   date      absent/present
1001     02/01/14  present
1002     02/01/14  present
1001     03/01/14  present
1002     03/01/14  absent

i tried

SELECT m.emp_id,t.date FROM timesheet_tran as t RIGHT OUTER JOIN emp_master m ON t.date BETWEEN '02/01/14' AND '04/01/14' AND t.emp_id = m.emp_id

but I need records for each date mentioned.

SELECT m.emp_id,t.date 
FROM timesheet_tran as t 
RIGHT OUTER JOIN emp_master m ON t.date 
AND t.date BETWEEN '2014-02-01' AND '2014-04-01'
 AND t.emp_id = m.emp_id

EDIT: From your updated comments, you need to query a join for all users in a manner that they will be listed whether they are present or not.

SELECT m.emp_id,t.date 
FROM emp_master as m 
LEFT JOIN timesheet_tran t ON date

This will list all users entries. Where the timesheet data is not present, a null will be returned. You can then convert this to string absent/present as you see fit.

    SELECT m.emp_id, 
CASE WHEN t.date IS NOT NULL 
       THEN 'present'
       ELSE 'absent'
END AS T_date
FROM emp_master as m 
LEFT JOIN timesheet_tran t ON date

Try Like this

Select emp_id,emp_name,Date1,(
CASE WHEN EXISTS(
sELECT emp_id FROM timesheet_tran AT WHERE T.emp_id =AT.emp_id AND T.date1=AT.date
) then 'Present' Else 'Absent' End )as Status
FROM
(
Select emp_id ,emp_name,Cast(date as DATE)AS DATE1 from emp_master a,(Select Distinct date    from timesheet_tran ) b
) T

SQL FIDDLE DEMO

Hope this help you

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