简体   繁体   中英

How to select data from last 30 days and include empty rows

I want to get the last 1 month attendance from database (Table a_days , HrEmployee and HrAttLogsFormatted ) when it is empty or not. When the data is empty I want to display "-" when there is display data from LogsFormatted.DateIn , and it can be in WHERE BETWEEN with (Ymd) date format. (Example from 2018-06-01 to 2018-06-10 )

Query :

SELECT
    Employee.Id as "EmployeeId",
    Employee.Name as "Name",
    TheDay.Days as "Date",
    LogsFormatted.DateIn as "DateIn",
    LogsFormatted.ScanIn as "ScanIn"
FROM
    HrEmployee as Employee
    LEFT JOIN HrEmployeeShift as EmployeeShift ON Employee.ShiftId = EmployeeShift.Id
    LEFT JOIN HrAttMachine as Machine ON Employee.MachineIP = Machine.IP
    LEFT JOIN HrAttLogsFormatted as LogsFormatted ON LogsFormatted.FingerId = Employee.Id
    LEFT JOIN a_days as TheDay ON TheDay.Days = DATE_FORMAT(LogsFormatted.DateIn, "%d")
WHERE Employee.ShiftId = EmployeeShift.Id
AND Employee.Id = '14522228'
ORDER BY Employee.Id, Employee.Name, TheDay.Days, LogsFormatted.DateIn ASC

Result:

FingerId    Name                Date   DateIn        ScanIn
14522228    Aldan Rizki Santosa 3      2018-07-06    06:45:54
14522228    Aldan Rizki Santosa 4      2018-07-09    06:38:12
14522228    Aldan Rizki Santosa 5      2018-07-10    06:48:35

What I want:

FingerId    Name                Date   DateIn        ScanIn
14522228    Aldan Rizki Santosa 1      -             -
14522228    Aldan Rizki Santosa 2      -             -
14522228    Aldan Rizki Santosa 3      2018-07-06    06:45:54
14522228    Aldan Rizki Santosa 4      2018-07-09    06:38:12
14522228    Aldan Rizki Santosa 5      2018-07-10    06:48:35
14522228    Aldan Rizki Santosa 6      -             -
.....
..... 
..... Until the date at the end of the month

Can you try the below query and check?

 SELECT EmployeeDetail.EmployeeId ,EmployeeDetail.Name ,TheDay.rn AS Days ,EmployeeDetail.DateIn ,EmployeeDetail.ScanIn FROM (SELECT TOP (DATEDIFF(DAY, '2018-01-01', DATEADD(MONTH,1,'2018-01-01'))) rn = ROW_NUMBER() OVER (ORDER BY s1.[object_id]) FROM sys.all_objects AS s1 CROSS JOIN sys.all_objects AS s2 ORDER BY s1.[object_id] ) TheDay OUTER APPLY (SELECT Employee.Id as EmployeeId, Employee.Name as Name, TheDay.rn as Date, LogsFormatted.DateIn as "DateIn", LogsFormatted.ScanIn as "ScanIn" FROM HrEmployee as Employee LEFT JOIN HrEmployeeShift as EmployeeShift ON Employee.ShiftId = EmployeeShift.Id LEFT JOIN HrAttMachine as Machine ON Employee.MachineIP = Machine.IP LEFT JOIN HrAttLogsFormatted as LogsFormatted ON LogsFormatted.FingerId = Employee.Id WHERE LogsFormatted.DateIn = TheDay.rn) AS EmployeeDetail WHERE EmployeeDetail.EmployeeId = '14522228' ORDER BY EmployeeDetail.Id, EmployeeDetail.Name, TheDay.Days, EmployeeDetail.DateIn ASC 

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