如何选择最近30天的数据并包括空行

时间:2018-07-16 07:01:46

标签: mysql sql

无论数据库是否为空,我都希望从数据库(表a_daysHrEmployeeHrAttLogsFormatted)获得最近1个月的出勤率。当数据为空时,我想在显示LogsFormatted.DateIn的显示数据时显示“-”,它可以是WHERE BETWEEN,日期格式为(Y-m-d)。 (示例从2018-06-012018-06-10

查询:

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

结果:

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

我想要的:

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

1 个答案:

答案 0 :(得分:1)

您可以尝试以下查询并检查吗?

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

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