我对此感到有点难过。我可以获得数小时或总分钟数,但我需要小时和分钟(如果工作时间超过60分钟)。开始时间和结束时间(datetime数据类型)存储在数据库中元组的字段中。
我需要花费数小时(如果超过60分钟)和剩余时间。我的SQL查询中有以下计算。我知道给我一个完整的分钟,我只需要在删除几个小时之后留下任何遗留物。
如果开始时间是上午11:15,结束时间是下午12:25,我需要1小时10分钟。
sqlQuery = "select distinct(job_#1) as 'Job Number', " +
"client_name as 'Client Name', " +
"Convert(varchar,min(date),101) as 'Start Date', " +
"convert(varchar, max(date), 101) as 'End Date', " +
"FORMAT(sum(datediff(mi, START_TIME,END_TIME)%(60*24)/60),'#','en-us' ) as 'Hours', " + // calculate the total time taken on the job - formatted numeric
"FORMAT(sum(datediff(mi, START_TIME,END_TIME)%(60*24)%60),'#','en-us' ) as 'Minutes', " + // calculate the total time taken on the job - formatted numeric
"FORMAT(sum((datediff(MINUTE,start_time,end_time))/60.00)*25.00, 'C', 'en-us') as 'Total Labor' " + // multiple the job time by a $$ per hour rate - formatted as currency
"from " + ssqltable + " " + // table from which data is pulled
"where job_#1 IS NOT NULL " + // find all jobs that have a job number leaving out blank job numbers
"and date>='" + myDate + "' " + // filled in when selected or passes null and shows all data
"group by job_#1, Client_Name " + // aggregate grouping
"order by job_#1;"; // column(s) to order the data
以下是我的输出示例..
237452 AADC 05/18/2017 05/18/2017 1 **208** $111.67
237353 Wolverine 05/18/2017 05/18/2017 **110** $45.83
237492 Beeman 05/11/2017 05/16/2017 **74** $30.83
答案 0 :(得分:2)
您可以datediff()
使用hour
几小时(如果您没有汇总),minute
分钟,并根据需要划分或模数:
select
[Job Number] = [job_#1]
, [Client Name]= client_name
, [Start Date] = convert(char(10),min(date),101)
, [End Date] = convert(char(10),max(date),101)
, Hours = sum(datediff(minute,Start_Time,End_Time))/60
, Minutes = sum(datediff(minute,Start_Time,End_Time))%60
, [Total Labor]= (sum(datediff(minute,Start_Time,End_Time))/60.00) * 25.00
from tbl
where job_#1 is not null
and [date] >= @date_parameter
group by [job_#1], client_name
order by [job_#1]
注意:
varchar
without (length) - Aaron Bertrand format()
效果非常糟糕:format()
is nice and all, but… - Aaron Bertrand 答案 1 :(得分:1)
如果你有总分钟数,那么DIV(/)和MOD / Remainder(%)函数就可以了。
DECLARE @TotalMinutes int
SET @TotalMinutes = 70
SELECT @TotalMinutes / 60 AS Hours,
@TotalMinutes % 60 AS Minutes
返回以下结果:
Total Minutes Hours Minutes
70 1 10
121 2 1
答案 2 :(得分:0)
您的代码应如下所示。请注意括号中的小时和分钟的变化。首先按SUM计算所有分钟,然后除以或取模60(整数)得到小时和分钟。
sqlQuery = "select distinct(job_#1) as 'Job Number', " +
"client_name as 'Client Name', " +
"Convert(varchar,min(date),101) as 'Start Date', " +
"convert(varchar, max(date), 101) as 'End Date', " +
"FORMAT(sum(datediff(mi, START_TIME,END_TIME)/60),'#','en-us' ) as 'Hours', " + // calculate the total time taken on the job - formatted numeric
"FORMAT(sum(datediff(mi, START_TIME,END_TIME))%60),'#','en-us' ) as 'Minutes', " + // calculate the total time taken on the job - formatted numeric
"FORMAT(sum(datediff(MINUTE,start_time,end_time)/60.00)*25.00, 'C', 'en-us') as 'Total Labor' " + // multiple the job time by a $$ per hour rate - formatted as currency
"from " + ssqltable + " " + // table from which data is pulled
"where job_#1 IS NOT NULL " + // find all jobs that have a job number leaving out blank job numbers
"and date>='" + myDate + "' " + // filled in when selected or passes null and shows all data
"group by job_#1, Client_Name " + // aggregate grouping
"order by job_#1;"; // column(s) to order the data
答案 3 :(得分:0)
我认为简单的除以60足够如下:
sum( datediff(mi,START_TIME,END_TIME)/60 ) as [Hours]
sum( datediff(mi,START_TIME,END_TIME)%60 ) as [Minutes]
格式效果不高。对于您的方案,您可以避免