我的系统中有这些数据。将持续时间列设为NVARCHAR
类型。
| Site | Application | Time Checked | Duration |
------------------------------------------------------
A AAAA 1/11/2016 12:00AM 5.0s
B AAAA 1/11/2016 12:00AM 4.0s
A BBBB 1/11/2016 12:00AM 5.0s
B BBBB 1/11/2016 12:00AM 4.0s
A AAAA 1/11/2016 12:30AM 5.0s
B AAAA 1/11/2016 12:30AM 4.0s
A BBBB 1/11/2016 12:30AM 5.0s
B BBBB 1/11/2016 12:30AM 4.0s
......................................................
A AAAA 1/11/2016 11:00PM 5.0s
B AAAA 1/11/2016 11:00PM 4.0s
A BBBB 1/11/2016 11:00PM 5.0s
B BBBB 1/11/2016 11:00PM 4.0s
A AAAA 1/11/2016 11:30PM 5.0s
B AAAA 1/11/2016 11:30PM 4.0s
A BBBB 1/11/2016 11:30PM 5.0s
B BBBB 1/11/2016 11:30PM 4.0s
A AAAA 1/12/2016 12:00AM 5.0s
B AAAA 1/12/2016 12:00AM 4.0s
A BBBB 1/12/2016 12:00AM 5.0s
B BBBB 1/12/2016 12:00AM 4.0s
A AAAA 1/12/2016 12:30AM 5.0s
B AAAA 1/12/2016 12:30AM 4.0s
A BBBB 1/12/2016 12:30AM 5.0s
B BBBB 1/12/2016 12:30AM 4.0s
......................................................
A AAAA 1/12/2016 11:00PM 5.0s
B AAAA 1/12/2016 11:00PM 4.0s
A BBBB 1/12/2016 11:00PM 5.0s
B BBBB 1/12/2016 11:00PM 4.0s
A AAAA 1/12/2016 11:30PM 5.0s
B AAAA 1/12/2016 11:30PM 4.0s
A BBBB 1/12/2016 11:30PM 5.0s
B BBBB 1/12/2016 11:30PM 4.0s
我的系统每天检查持续时间每小时2次(从NN开始:00 AM / PM,接下来是NN:30 AM / PM)我需要获得不同站点每天的持续时间的总平均值和整个月的申请。
喜欢这个
| (Site A) Ave.Duration | Day Time Checked | Application |
5.4 s 1/11/2016 AAAA
5.4 s 1/12/2016 AAAA
5.4 s 1/13/2016 AAAA
..........................................................
5.4 s 1/11/2016 BBBB
5.4 s 1/12/2016 BBBB
5.4 s 1/13/2016 BBBB
..........................................................
有人可以帮我这个吗?我已经在这里遇到了不同的问题。
但我还是不太明白。
你的回答将会很有帮助。
答案 0 :(得分:0)
试试这个:一个完整的Demo
create table
#test1
(
site char(1),
apl char(10),
timechecked datetime,
duration real,
)
insert into #test1
select
'A','AAAA','1/11/2016 12:00AM',5.0s
union all
select
'B','BBBB','1/11/2016 12:30AM',4.0s
union all
select
'A','BBBB','1/12/2016 12:00AM',5.0s
--nromal method
;with cte
as
(
select duration,convert(varchar(2),datepart(day,timechecked))+'/'+
convert(varchar(2),datepart(month,timechecked))+'/'+
convert(varchar(4),datepart(year,timechecked)) as datetimee,
apl
from
#test1
)
select
avg(duration) as avgduration,
datetimee,apl
from
cte
group by datetimee,apl
---using window functions
;with cte
as
(
select duration,convert(varchar(2),datepart(day,timechecked))+'/'+
convert(varchar(2),datepart(month,timechecked))+'/'+
convert(varchar(4),datepart(year,timechecked)) as datetimee,
apl
from
#test1
)
select
avg(duration) over (partition by datetimee order by datetimee ) as avgduration,
datetimee,
apl
from cte