在SQL Server中使用DATEDIFF进行除法和减法?

时间:2015-05-07 08:50:42

标签: sql-server sql-server-2012

[AttenPerc] 的出勤率中计算错误的计算将从([TotallectdHoursForStu] / [TotalAttenHoursForStu])* 100 计算得出。 [AbsentHours] 中的错误小时数我想从[TotallectdHoursForStu] 中减去 [TotalAttenHoursForStu]以获得 hh:mm:ss 的确切差异, [AbsPerc] 中的缺席百分比将从([AbsentHours] / [TotalAttenHoursForStu])* 100 计算得出。查看报告结束代码,您将理解我的意思。您会在第一行注意到撒字点 -2.310 ,其中 57.142%,AbsentHours 22:36:00 01:22:00 ,AbsPerc为102.31, 42.85%。希望你理解我的问题..

USE [dbmairTime]
GO
/****** Object:  StoredProcedure [dbo].[StudentReports]    Script Date: 5/7/2015 09:29:47 ص ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[StudentReports]  @StuColNum nvarchar(50)
as
select subjectname,Subjects.totalHours,
CAST(DATEADD(mi,SUM(CAST(DATEDIFF(MI,COALESCE(StudentsAttendance.AttendTime,StudentsAttendance.LeaveTime),StudentsAttendance.LeaveTime) as float)),'00:00')as time(0))as [TotalAttenHoursForStu],
CAST(DATEADD(mi,SUM(CAST(DATEDIFF(MI,LecturerAttendance.AttenedTime,LecturerAttendance.LeaveTime) as float)),'00:00')as time(0)) as [TotallectdHoursForStu],

/**** The Percentage isn't right ***/
CAST(((SUM(CAST(DATEDIFF(MI,COALESCE(StudentsAttendance.AttendTime,StudentsAttendance.LeaveTime),StudentsAttendance.LeaveTime) as float))/SUM(CAST(DATEDIFF(MI,COALESCE(StudentsAttendance.AttendTime,StudentsAttendance.LeaveTime),LecturerAttendance.LeaveTime) as float))))*100 as float)as [AttenPerc],

/**** Absent Hours isn't right ***/
CAST(DATEADD(mi,(SUM(CAST(DATEDIFF(MI,COALESCE(LecturerAttendance.AttenedTime,LecturerAttendance.LeaveTime),LecturerAttendance.LeaveTime) as float))-SUM(CAST(DATEDIFF(MI,COALESCE(StudentsAttendance.AttendTime,StudentsAttendance.LeaveTime),StudentsAttendance.LeaveTime) as float))),'00:00') as time(0)) as [AbsentHours],

/***** Percentage isn't right *****/
100-CAST(((SUM(CAST(DATEDIFF(MI,COALESCE(StudentsAttendance.AttendTime,StudentsAttendance.LeaveTime),StudentsAttendance.LeaveTime) as float))/SUM(CAST(DATEDIFF(MI,COALESCE(StudentsAttendance.AttendTime,StudentsAttendance.LeaveTime),LecturerAttendance.LeaveTime) as float))))*100 as float) as [AbsPerc]
from StudentsAttendance inner join Subjects
on Subjects.subjectid=StudentsAttendance.Subject_ID
inner join LecturerAttendance
on LecturerAttendance.SubjectID=StudentsAttendance.Subject_ID and LecturerAttendance.DivisonID=StudentsAttendance.Division_ID
where StudentsAttendance.StudentCollNumber=@StuColNum 
group by Subject_ID,Subjects.totalHours,subjectname 

以下是我使用学生ID执行StudentReport时的报告示例

[StudentReports] 431041282

   subjectname  totalHours  TotalAttenHoursForStu   TotallectdHoursForStu   AttenPerc           AbsentHours  AbsPerc
    S111        50          03:16:00                 01:52:00               -2.31023102310231    22:36:00    102.310231023102
    M222        50          00:11:00                 00:11:00                100                 00:00:00        0

0 个答案:

没有答案