我必須從下表中找出每週平均值(僅工作日 - 星期一至星期五)。我搜索的平均數是AVAI_NUM。我必須計算給定期間內每週的AVAI_NUM平均值。獲取每週工作日的平均每週
例子: 如果期限爲2016年1月:
declare @dDateFrom date = cast('2016-01-01' as date)
declare @dDateTo date = cast('2016-01-31' as date)
表看起來是這樣的:
declare @scavai table
(
avai_key int,
avai_dat date,
avai_num int
)
填寫表格與數據:
insert into @scavai(avai_Key, avai_dat, avai_num)
select 1, cast('2016-01-01' as date), 3
union all
select 1, cast('2016-01-02' as date), 2
union all
select 1, cast('2016-01-03' as date), 5
union all
select 1, cast('2016-01-04' as date), 6
union all
select 1, cast('2016-01-05' as date), 1
union all
select 1, cast('2016-01-06' as date), 8
union all
select 1, cast('2016-01-07' as date), 4
union all
select 1, cast('2016-01-08' as date), 3
union all
select 1, cast('2016-01-09' as date), 2
union all
select 1, cast('2016-01-10' as date), 1
union all
select 1, cast('2016-01-11' as date), 9
union all
select 1, cast('2016-01-12' as date), 7
union all
select 1, cast('2016-01-13' as date), 3
union all
select 1, cast('2016-01-14' as date), 8
union all
select 1, cast('2016-01-15' as date), 3
union all
select 1, cast('2016-01-16' as date), 2
union all
select 1, cast('2016-01-17' as date), 3
union all
select 1, cast('2016-01-18' as date), 1
union all
select 1, cast('2016-01-19' as date), 4
union all
select 1, cast('2016-01-20' as date), 5
union all
select 1, cast('2016-01-21' as date), 7
union all
select 1, cast('2016-01-22' as date), 3
union all
select 1, cast('2016-01-23' as date), 3
union all
select 1, cast('2016-01-25' as date), 3
union all
select 1, cast('2016-01-26' as date), 3
我知道如何計算數量在特定時期內的週數:
select DATEDIFF(ww, '2016-01-01', '2016-01-31')
但是我不知道如何計算每週工作日的AVAI_NUM,並將其與特定周的天數相除,然後總結所有這些並用週數除?
嘗試AVG(avai_num)以上(分區的日期部分(周,avai_dat)由avai_dat順序) – nazark
@nazark - 你可以請張貼這個作爲答案? (如果可能的話,整個查詢就是例子)。謝謝。 – FrenkyB
已發佈但不確定你要找的結果? – nazark