我试图用另一个表中的多个记录填充一个表。 我有一个表,其中包含员工时间表的记录。 每条记录包含一个事件的日期和时间,该事件涉及1名员工: 字段是:
ScheduleID = unique record identifier
ScheduleName = identifies the schedule
ScheduleLine = identifies the line on the schedule grid
Position = the schedule employee position filled (not necessarily the
same as the employees postion)
ADP_ID = employee identifier
ScheduleBeginDay = what day of the week the schedule begins (usually
Monday)
ShiftName = shift worked
ScheduleStart = date and time employee is scheduled to clock in
ScheduleEnd = date and time employee is scheduled to clock out
RecordActive = is record active or deleted
我需要将其转换为看起来像应用程序以及报表中的时间表网格。 那是网格中的1行=最多7条先前的记录(周一至周日)
样本输入数据: ScheduleName ScheduleBeginDay ShiftName ScheduleStart ScheduleEnd位置ADP_ID ScheduleLine 步入中心2019年4月1日星期一1班次2019-04-01 07:00:00.000 2019-04-01 15:30:00.000心理健康科技10391 1 步入式中心2019年4月1日星期一1班次2019-04-02 07:00:00.000 2019-04-02 15:30:00.000心理健康科技10391 1 步入中心2019年4月1日星期一1班次2019-04-03 07:00:00.000 2019-04-03 15:30:00.000心理健康科技10391 1 步入中心2019年4月1日星期一1班次2019-04-04 07:00:00.000 2019-04-04 15:30:00.000心理健康科技10391 1 步入式中心2019年4月1日星期一1班次2019-04-05 07:00:00.000 2019-04-05 15:30:00.000心理健康科技10391 1 步入式中心2019年4月1日星期六星期一第一班2019-04-06 07:00:00.000 2019-04-06 15:30:00.000 Mental Health Tech 10244 1
样本输出数据: ScheduleLine ScheduleName ScheduleBeginDay ShiftName ScheduleStart ScheduleEnd位置ADP_ID FirstName姓氏ADP_ID FirstName姓氏ADP_ID FirstName姓氏ADP_ID FirstName姓氏ADP_ID FirstName姓氏ADP_ID FirstName姓氏ADP_ID FirstName姓氏 1步入式中心2019年4月1日星期一1班次2019-04-01 07:00:00.000 2019-04-01 15:30:00.000心理健康技术10391 Arthur Davis 10391 Arthur Davis 10391 Arthur Davis 10391 Arthur Davis 10391 Arthur Davis 10244埃里克·哈林10244埃里克·哈林
以下代码有效,但仅当第一天有人在该位置时才有效。 通常但并非总是如此。 问题是“我如何在时间表上为每行(职位)创建1行输出(只要至少1个人在一周中至少一天在该职位上至少工作1天)?
declare @startdate as datetime
set @startdate = '4/1/19'
select
day1.ScheduleLine,
day1.ScheduleName,
day1.ScheduleBeginDay,
day1.ShiftName,
day1.ScheduleStart,
day1.ScheduleEnd,
day1.Position,
day1.ADP_ID,
Emp1.FirstName,
Emp1.LastName,
day2.ADP_ID,
Emp2.FirstName,
Emp2.LastName,
day3.ADP_ID,
Emp3.FirstName,
Emp3.LastName,
day4.ADP_ID,
Emp4.FirstName,
Emp4.LastName,
day5.ADP_ID,
Emp5.FirstName,
Emp5.LastName,
day6.ADP_ID,
Emp6.FirstName,
Emp6.LastName,
day7.ADP_ID,
Emp7.FirstName,
Emp7.LastName
from @startDate
left outer join Schedules day1
left outer join EmployeeInformation Emp1 on Emp1.ADP_ID = day1.ADP_ID
left outer join Schedules day2 on day2.ScheduleName = day1.ScheduleName
and day2.ScheduleLine = day1.ScheduleLine and day2.ShiftName =
day1.ShiftName and day2.Position = day1.Position and convert(varchar(10),
day2.ScheduleStart, 101) = CONVERT(varchar(10), @startdate + 1,101)
left outer join EmployeeInformation Emp2 on Emp2.ADP_ID = day2.ADP_ID
left outer join Schedules day3 on day3.ScheduleName = day1.ScheduleName
and day3.ScheduleLine = day1.ScheduleLine and day3.ShiftName =
day1.ShiftName and day3.Position = day1.Position and convert(varchar(10),
day3.ScheduleStart, 101) = CONVERT(varchar(10), @startdate + 2,101)
left outer join EmployeeInformation Emp3 on Emp3.ADP_ID = day3.ADP_ID
left outer join Schedules day4 on day4.ScheduleName = day1.ScheduleName
and day4.ScheduleLine = day1.ScheduleLine and day4.ShiftName =
day1.ShiftName and day4.Position = day1.Position and convert(varchar(10),
day4.ScheduleStart, 101) = CONVERT(varchar(10), @startdate + 3,101)
left outer join EmployeeInformation Emp4 on Emp4.ADP_ID = day4.ADP_ID
left outer join Schedules day5 on day5.ScheduleName = day1.ScheduleName
and day5.ScheduleLine = day1.ScheduleLine and day5.ShiftName =
day1.ShiftName and day5.Position = day1.Position and convert(varchar(10),
day5.ScheduleStart, 101) = CONVERT(varchar(10), @startdate + 4,101)
left outer join EmployeeInformation Emp5 on Emp5.ADP_ID = day5.ADP_ID
left outer join Schedules day6 on day6.ScheduleName = day1.ScheduleName
and day6.ScheduleLine = day1.ScheduleLine and day6.ShiftName =
day1.ShiftName and day6.Position = day1.Position and convert(varchar(10),
day6.ScheduleStart, 101) = CONVERT(varchar(10), @startdate + 5,101)
left outer join EmployeeInformation Emp6 on Emp6.ADP_ID = day6.ADP_ID
left outer join Schedules day7 on day7.ScheduleName = day1.ScheduleName
and day7.ScheduleLine = day1.ScheduleLine and day7.ShiftName =
day1.ShiftName and day7.Position = day1.Position and convert(varchar(10),
day7.ScheduleStart, 101) = CONVERT(varchar(10), @startdate + 6,101)
left outer join EmployeeInformation Emp7 on Emp7.ADP_ID = day7.ADP_ID
where convert(varchar(10), day1.ScheduleStart, 101) =
CONVERT(varchar(10), @startdate, 101)
order by day1.ScheduleLine
答案 0 :(得分:0)
此代码用于根据个人记录创建每周计划。 在实际的实时代码中,实际上将从屏幕上的字段或计算出的字段填充硬编码的字符串(时间表名称,开始日期)
--create a temp table to hold basic data for each line on the schedule
--this is referred to as day0 and will later be joined with data from days 1 - 7
CREATE TABLE #temp_dates (
ScheduleName VARCHAR(MAX),
ScheduleLine int,
ScheduleBeginDay VARCHAR(MAX),
ShiftName VARCHAR(MAX),
Position VARCHAR(MAX),
ScheduleStart datetime,
ScheduleEnd datetime
);
--create 1 temp record for each line on the schedule and populate the basic data
insert into #temp_dates
(ScheduleName,ScheduleLine,ShiftName,Position,ScheduleBeginDay)
select distinct ScheduleName,ScheduleLine,ShiftName,Position,ScheduleBeginDay
from Schedules
where ScheduleName = 'Walk-In Center April Wk 1 2019'
group by ScheduleName,ScheduleLine,ShiftName,Position,ScheduleBeginDay
order by ScheduleName,ScheduleLine,ShiftName,Position,ScheduleBeginDay
-- set date for start of weekly schedule
declare @startdate as datetime
set @startdate = '04/01/2019'
--populate the schedule start and end times for each line
update #temp_dates
set ScheduleStart = @startdate + cast(Shifts.WorkTimeStart as datetime) , ScheduleEnd = @startdate + cast(Shifts.WorkTimeEnd as datetime)
from #temp_dates
inner join Shifts
on shifts.ShiftName = #temp_dates.shiftname
--merge each temp table line with each days data for the week
USE CCAP
select
day0.ScheduleName,
day0.ScheduleLine,
day0.ShiftName,
day0.Position,
day0.ScheduleBeginDay,
format(day0.ScheduleStart,'hh:mm') as StartTime,
format(day0.ScheduleEnd,'hh:mm') as EndTime,
day0.Position,
day1.ADP_ID,
Emp1.FirstName,
Emp1.LastName,
day2.ADP_ID,
Emp2.FirstName,
Emp2.LastName,
day3.ADP_ID,
Emp3.FirstName,
Emp3.LastName,
day4.ADP_ID,
Emp4.FirstName,
Emp4.LastName,
day5.ADP_ID,
Emp5.FirstName,
Emp5.LastName,
day6.ADP_ID,
Emp6.FirstName,
Emp6.LastName,
day7.ADP_ID,
Emp7.FirstName,
Emp7.LastName
from #temp_dates day0
left outer join Schedules day1 on day1.ScheduleName = day0.ScheduleName and day1.ScheduleLine = day0.ScheduleLine and day1.ShiftName = day0.ShiftName and day1.Position = day0.Position and convert(varchar(10), day1.ScheduleStart, 101) = CONVERT(varchar(10), day0.ScheduleStart,101)
left outer join EmployeeInformation Emp1 on Emp1.ADP_ID = day1.ADP_ID
left outer join Schedules day2 on day2.ScheduleName = day0.ScheduleName and day2.ScheduleLine = day0.ScheduleLine and day2.ShiftName = day0.ShiftName and day2.Position = day0.Position and convert(varchar(10), day2.ScheduleStart, 101) = CONVERT(varchar(10), day0.ScheduleStart + 1,101)
left outer join EmployeeInformation Emp2 on Emp2.ADP_ID = day2.ADP_ID
left outer join Schedules day3 on day3.ScheduleName = day0.ScheduleName and day3.ScheduleLine = day0.ScheduleLine and day3.ShiftName = day0.ShiftName and day3.Position = day0.Position and convert(varchar(10), day3.ScheduleStart, 101) = CONVERT(varchar(10), day0.ScheduleStart + 2,101)
left outer join EmployeeInformation Emp3 on Emp3.ADP_ID = day3.ADP_ID
left outer join Schedules day4 on day4.ScheduleName = day0.ScheduleName and day4.ScheduleLine = day0.ScheduleLine and day4.ShiftName = day0.ShiftName and day4.Position = day0.Position and convert(varchar(10), day4.ScheduleStart, 101) = CONVERT(varchar(10), day0.ScheduleStart + 3,101)
left outer join EmployeeInformation Emp4 on Emp4.ADP_ID = day4.ADP_ID
left outer join Schedules day5 on day5.ScheduleName = day0.ScheduleName and day5.ScheduleLine = day0.ScheduleLine and day5.ShiftName = day0.ShiftName and day5.Position = day0.Position and convert(varchar(10), day5.ScheduleStart, 101) = CONVERT(varchar(10), day0.ScheduleStart + 4,101)
left outer join EmployeeInformation Emp5 on Emp5.ADP_ID = day5.ADP_ID
left outer join Schedules day6 on day6.ScheduleName = day0.ScheduleName and day6.ScheduleLine = day0.ScheduleLine and day6.ShiftName = day0.ShiftName and day6.Position = day0.Position and convert(varchar(10), day6.ScheduleStart, 101) = CONVERT(varchar(10), day0.ScheduleStart + 5,101)
left outer join EmployeeInformation Emp6 on Emp6.ADP_ID = day6.ADP_ID
left outer join Schedules day7 on day7.ScheduleName = day0.ScheduleName and day7.ScheduleLine = day0.ScheduleLine and day7.ShiftName = day0.ShiftName and day7.Position = day0.Position and convert(varchar(10), day7.ScheduleStart, 101) = CONVERT(varchar(10), day0.ScheduleStart + 6,101)
left outer join EmployeeInformation Emp7 on Emp7.ADP_ID = day7.ADP_ID
order by day0.ScheduleLine,day0.ScheduleStart