You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel中生成指定日期区间内所有员工的缺勤报告?

Got it, let's tackle how to generate that absence report for all employees between two specified dates. Here's a practical, step-by-step SQL solution tailored to your attendance table structure:

Generate Employee Absence Report Between Two Dates

The core challenge here is identifying every employee-date pair in your target range that doesn't exist in the attendance table (those are the absences). We'll break this into manageable parts:

Step 1: Define Your Date Range & Generate All Dates

First, we'll create a list of every date between your start and end date using a recursive CTE (works for SQL Server; adjust syntax if using PostgreSQL/MySQL):

DECLARE @StartDate DATE = '2024-01-01'; -- Replace with your start date
DECLARE @EndDate DATE = '2024-01-15';   -- Replace with your end date

WITH DateRange AS (
    SELECT @StartDate AS DateValue
    UNION ALL
    SELECT DATEADD(DAY, 1, DateValue)
    FROM DateRange
    WHERE DateValue < @EndDate
),

Step 2: Get the Full List of Employees

Next, pull all unique employees from your attendance table (if you have a dedicated Employees table, use that instead—it's more reliable):

AllEmployees AS (
    SELECT DISTINCT id AS EmployeeId, name AS EmployeeName
    FROM attendance_table
)

Step 3: Find Missing Attendance Entries (Absences)

Cross-join the date range with employees to get every possible employee-date pair, then left-join with the attendance table to filter out dates where the employee was present:

SELECT 
    ae.EmployeeId,
    ae.EmployeeName,
    dr.DateValue AS AbsenceDate
FROM DateRange dr
CROSS JOIN AllEmployees ae
LEFT JOIN attendance_table att
    ON ae.EmployeeId = att.id 
    AND dr.DateValue = att.date
WHERE att.id IS NULL -- Filter out dates where attendance was recorded
ORDER BY ae.EmployeeName, dr.DateValue;

Key Notes:

  • Dedicated Employees Table: If you have a separate Employees table (best practice), replace the AllEmployees CTE with SELECT EmployeeId, EmployeeName FROM Employees.
  • Database-Specific Adjustments: For PostgreSQL, use generate_series to create the date range. For MySQL, use a recursive CTE or a pre-built numbers table.
  • Output: The result will list every employee and every date in your range where they have no entry in the attendance table.

Example Output:

EmployeeIdEmployeeNameAbsenceDate
101Alice Brown2024-01-03
101Alice Brown2024-01-08
102Bob Miller2024-01-02

内容的提问来源于stack exchange,提问作者Include13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:57:46