我对SQL很新。这是我的最终目标:
2015年12月31日之后由护理人员按日历日工作的小时数
这是我现在的代码
select br_ID, car_ID, car_FirstName, car_LastName, car_SkillCd, car_Role, car_EmpSubCod_UD, schst_Name, svcc_Name, dbo.ConvertTimeFromUtc(sch_StartTime , tz_Data), dbo.ConvertTimeFromUtc(sch_endtime , tz_Data), sch_Units, sch_PayDate, sch_PayUnits, cli_LastName, cli_FirstName
from T_Schedules
inner join T_Branches on br_ID = sch_BranchID
inner join T_TimeZones on tz_ID = br_TimeZoneID
Full outer join T_Caregiver ON T_schedules.sch_CaregiverID = T_Caregiver.car_ID
full outer join T_ServiceCode on T_Schedules.sch_ServiceCodeID = T_ServiceCode.svcc_ID
full outer join T_ScheduleStatus on T_schedules.sch_ScheduleStatusID = T_ScheduleStatus.schst_ID
full outer join T_Client on T_Schedules.sch_ClientID = T_Client.cli_ID
where sch_ScheduleStatusID = 3 and dbo.ConvertTimeFromUtc(sch_StartTime , tz_Data) >= '2016-01-01'
任何人都知道如何按照照顾者分组。我不能只计算预定的单位,因为如果它是一夜之间的班次,那么这些时间可以在2个日历日之间分配。
答案 0 :(得分:0)
隔夜转移是一个问题,很可能会扭曲你的结果。 除非你回去并将1/2转换为一天和1/2到另一天,否则SQL无法为你做任何事情。 (我认为)我将如何处理这个问题是一个简单的小组,看看它是否足够好:
SELECT dbo.ConvertTimeFromUtc(sch_StartTime , tz_Data),
br_ID, car_ID, car_FirstName,
car_LastName, car_SkillCd, car_Role,
car_EmpSubCod_UD, schst_Name, svcc_Name,
dbo.ConvertTimeFromUtc(sch_endtime , tz_Data),
SUM(sch_Units),
sch_PayDate, sch_PayUnits, cli_LastName, cli_FirstName
FROM T_Schedules
INNER JOIN T_Branches on br_ID = sch_BranchID
INNER JOIN T_TimeZones on tz_ID = br_TimeZoneID
FULL OUTER JOIN T_Caregiver ON T_schedules.sch_CaregiverID = T_Caregiver.car_ID
FULL OUTER JOIN T_ServiceCode on T_Schedules.sch_ServiceCodeID = T_ServiceCode.svcc_ID
FULL OUTER JOIN T_ScheduleStatus on T_schedules.sch_ScheduleStatusID = T_ScheduleStatus.schst_ID
FULL OUTER JOIN T_Client on T_Schedules.sch_ClientID = T_Client.cli_ID
WHERE sch_ScheduleStatusID = 3 and dbo.ConvertTimeFromUtc(sch_StartTime , tz_Data) >= '2016-01-01'
GROUP BY [sch_StartTime]
答案 1 :(得分:0)
问题:从sch_StartTime
到sch_endtime
的转变可能会在午夜过后,所以一天工作一小时,下一天工作一些。
我们写了两个查询:一个用于第一天,一个用于下一个,然后将两个集合与UNION ALL
粘合在一起。以下是使用WITH
子句逐步构建的查询:
with worked as
(
select
sch_caregiverid as caregiverid,
dbo.ConvertTimeFromUtc(sch_StartTime, tz_Data) as starttime,
dbo.ConvertTimeFromUtc(sch_endtime, tz_Data) as endtime
from T_Schedules
where sch_ScheduleStatusID = 3 and dbo.ConvertTimeFromUtc(sch_StartTime , tz_Data) >= '2016-01-01'
)
, day1 as
(
select
caregiverid,
starttime,
case when day(starttime) <> day(endtime) then
datetimefromparts(year(starttime), month(starttime), day(starttime), 23, 59, 59, 999)
else
endtime
end as endtime
from worked
)
, day2 as
(
select
caregiverid,
datetimefromparts(year(endtime), month(endtime), day(endtime), 0, 0, 0, 0) as starttime,
endtime
from worked
where day(starttime) <> day(endtime)
)
, bothdays as
(
select caregiverid, starttime, endtime from day1
union all
select caregiverid, starttime, endtime from day2
)
, hours_worked as
(
select
caregiverid,
convert(date, starttime) as whichday,
sum(datediff(hour, starttime, endtime)) as total
from bothdays
group by caregiverid, convert(date, starttime)
)
select *
from t_caregiver
join hours_worked on hours_worked.caregiverid = t_caregiver.car_id;