如何汇总sql server中的时间列
我想总结星期一,星期二,星期三,星期四,星期五和星期六的时间,并更新到Tot_hrs我有更新逻辑我无法将星期一到星期六的hrs总结
CREATE TABLE WEEKLY_ATTN(
[SYSID] [int] IDENTITY(1,1) NOT NULL,
[EMPLOYEE_SYSID] [int] NULL,
[Mon_day] [varchar](10) NULL,
[Tue_day] [varchar](10) NULL,
[Wed_day] [varchar](10) NULL,
[Thu_day] [varchar](10) NULL,
[Fri_day] [varchar](10) NULL,
[Sat_day] [varchar](10) NULL,
[Tot_Hrs] [varchar](20) NULL
);
INSERT INTO WEEKLY_ATTN (employee_sysid,mon_day,tue_day,wed_day,thu_day,fri_day,sat_day)
values(41,'11:01','11:57','11:02','10:19','09:26',null);
以下是我使用的查询
select mon_day,Tue_day,wed_day,thu_day,fri_Day,sat_day,
cast(dateadd(s,
isnull(datediff(s, 0, mon_day), 0)+
isnull(datediff(s, 0, Tue_day), 0)+
isnull(datediff(s, 0, wed_day), 0)+
isnull(datediff(s, 0, Thu_day), 0)+
isnull(datediff(s, 0, fri_day), 0)+
isnull(datediff(s, 0, sat_day), 0)
,0) as time(0)) as Tot_hrs
from weekly_attn where employee_sysid=41;
mon_day Tue_day wed_day thu_day fri_Day sat_day tot_hrs
---------------------------------------------------------
11:01 11:57 11:02 10:19 09:26 NULL 05:45:00
然而总时间是53:45小时,任何人都可以纠正错误的地方。有什么帮助吗?
答案 0 :(得分:0)
varchar应该是时间。
select *,CONVERT(varchar, DATEADD(ms,Tot_hrs * 1000, 0),114) from
(select mon_day,Tue_day,wed_day,thu_day,fri_Day,sat_day,
isnull(cast(datediff(s, 0,cast( mon_day as time)) as float),0)+
isnull(cast(datediff(s, 0,cast( Tue_day as time))as float),0)+
isnull(cast(datediff(s, 0,cast( wed_day as time))as float),0)+
isnull(cast(datediff(s, 0,cast( Thu_day as time))as float),0)+
isnull(cast(datediff(s, 0,cast( fri_day as time))as float),0)+
isnull(cast(datediff(s, 0,cast( sat_day as time)) as float),0)
as Tot_hrs
from @weekly_attn where employee_sysid=41)tbl
答案 1 :(得分:0)
以下是一种总计数小时的方法
select
CAST(
CAST(SUBSTRING(isnull(mon_day,0),1,2) as int)+CAST(SUBSTRING(isnull(Tue_day,0),1,2) as int)+
CAST(SUBSTRING(isnull(wed_day,0),1,2) as int)+CAST(SUBSTRING(isnull(Thu_day,0),1,2) as int)+
CAST(SUBSTRING(isnull(fri_day,0),1,2) as int)+CAST(SUBSTRING(isnull(sat_day,0),1,2) as int)
+
(
CAST(SUBSTRING(isnull(mon_day,0),4,5) as int)+CAST(SUBSTRING(isnull(Tue_day,0),4,5) as int)+
CAST(SUBSTRING(isnull(wed_day,0),4,5) as int)+CAST(SUBSTRING(isnull(Thu_day,0),4,5) as int)+
CAST(SUBSTRING(isnull(fri_day,0),4,5) as int)+CAST(SUBSTRING(isnull(sat_day,0),4,5) as int)
)/60
AS varchar(50))
+':'+
CAST(
(
CAST(SUBSTRING(isnull(mon_day,0),4,5) as int)+CAST(SUBSTRING(isnull(Tue_day,0),4,5) as int)+
CAST(SUBSTRING(isnull(wed_day,0),4,5) as int)+CAST(SUBSTRING(isnull(Thu_day,0),4,5) as int)+
CAST(SUBSTRING(isnull(fri_day,0),4,5) as int)+CAST(SUBSTRING(isnull(sat_day,0),4,5) as int)
)%60
AS varchar(50))
from weekly_attn
输出:53:45