我有一张表,记录员工的出勤情况。我们只插入现在的日子。我想显示当月的所有日期以及记录的出勤日期。未记录的日期将显示缺席。需要帮忙。 这是表员工出勤率。请注意,我们只在此表中插入当前日期。缺席和假期未插入此表。
`CREATE TABLE [dbo].[EmployeeAttendance](
[CompanyCode] [int] NOT NULL,
[BranchCode] [int] NOT NULL,
[TransactionDate] [datetime] NOT NULL,
[EmployeeCode] [int] NOT NULL,
[AttendanceCount] [int] NOT NULL,
[ShiftCode] [int] NOT NULL,
[ScheduledTimeIn] [datetime] NULL,
[ScheduledTimeOut] [datetime] NULL,
[BreakStartTime] [datetime] NULL,
[BreakEndTime] [datetime] NULL,
[FlexiLateTime] [int] NULL,
[FlexiEarlyTime] [int] NULL,
[RecordedTimeIn] [datetime] NULL,
[RecordedTimeOut] [datetime] NULL,
[RemarksIn] [varchar](500) NULL,
[RemarksOut] [varchar](500) NULL,
[AddByUserId] [int] NULL,
[AddDateTimeIn] [datetime] NULL,
[AddDateTimeOut] [datetime] NULL,
CONSTRAINT [PK_EmployeeAttendance_1] PRIMARY KEY CLUSTERED
(
[CompanyCode] ASC,
[BranchCode] ASC,
[TransactionDate] ASC,
[EmployeeCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]`
答案 0 :(得分:0)
首先:你需要创建一个"表"这个月的所有日子都在里面。然后,您将记录的出勤数据加入到该日期范围内。
SELECT d.date, a.*
FROM [dates] AS d -- this is a set of rows, one per needed date
LEFT JOIN EmployeeAttendance AS a ON d.[date] = a.[RecordedTimeIn]
现在,根据所使用的数据库,您可以通过各种方式生成所需的日期行(例如,递归公用表表达式)。
无论方法如何,每个日期都需要一行是您的第一步,这将是"来自"表