4周資訊 按照 周 星期 統計查詢

2021-05-22 01:30:55 字數 3553 閱讀 4378

if object_id('tempdb..#temp') is not null--臨時表存在則刪除

begin

drop table #temp

endcreate table #temp--建立臨時表

(id int identity primary key,

columnname varchar(100),

week_1 varchar(100) default 0,

week_2 varchar(100) default 0,

week_3 varchar(100) default 0,

week_4 varchar(100) default 0

)--定義四周的日期

declare @d1 varchar(10)

declare @d11 varchar(10)

declare @d2 varchar(10)

declare @d22 varchar(10)

declare @d3 varchar(10)

declare @d33 varchar(10)

declare @d4 varchar(10)

declare @d44 varchar(10)

set @d1= convert(varchar(10), dateadd(wk, datediff(wk,0,getdate()), -21), 23)

set @d11= convert(varchar(10), dateadd(wk, datediff(wk,0,getdate()), -15), 23)

set @d2= convert(varchar(10), dateadd(wk, datediff(wk,0,getdate()), -14), 23)

set @d22= convert(varchar(10), dateadd(wk, datediff(wk,0,getdate()), -8), 23)

set @d3= convert(varchar(10), dateadd(wk, datediff(wk,0,getdate()), -7), 23)

set @d33= convert(varchar(10), dateadd(wk, datediff(wk,0,getdate()), -1), 23)

set @d4= convert(varchar(10), dateadd(wk, datediff(wk,0,getdate()), 0), 23)

set @d44= convert(varchar(10), dateadd(wk, datediff(wk,0,getdate()), 6), 23)

--生成表頭和左列資訊

insert into #temp (columnname,week_1,week_2,week_3,week_4) values ('',substring(@d1,6,11)+'至'+substring(@d11,6,11),substring(@d2,6,11)+'至'+substring(@d22,6,11),substring(@d3,6,11)+'至'+substring(@d33,6,11),substring(@d4,6,11)+'至'+substring(@d44,6,11))

insert into #temp (columnname) values ('星期一')

insert into #temp (columnname) values ('星期二')

insert into #temp (columnname) values ('星期三')

insert into #temp (columnname) values ('星期四')

insert into #temp (columnname) values ('星期五')

insert into #temp (columnname) values ('星期六')

insert into #temp (columnname) values ('星期日')

insert into #temp (columnname) values ('周排行成績')

insert into #temp (columnname) values ('名次')

insert into #temp (columnname) values ('獲得獎勵')

insert into #temp (columnname) values ('領取獎勵')

--統計資訊

--周一至週日的成績

declare @i int

set @i = 0

while @i < 7

begin

update #temp set week_1=(select (case when (sum(bean) is null) then 0 else sum(bean) end) from match where datediff(day,matchtime,(convert(datetime,@d1)+@i))=0),

week_2=(select (case when (sum(bean) is null) then 0 else sum(bean) end) from match where datediff(day,matchtime,(convert(datetime,@d2)+@i))=0),

week_3=(select (case when (sum(bean) is null) then 0 else sum(bean) end) from match where datediff(day,matchtime,(convert(datetime,@d3)+@i))=0),

week_4=(select (case when (sum(bean) is null) then 0 else sum(bean) end) from match where datediff(day,matchtime,(convert(datetime,@d4)+@i))=0)

where id=(@i+2)

print (@i+2)

set @i =@i +1

end--周排行成績

update #temp set week_1=(select (case when (sum(bean) is null) then 0 else sum(bean) end) from match where datediff(week,matchtime,@d1)=0),

week_2=(select (case when (sum(bean) is null) then 0 else sum(bean) end) from match where datediff(week,matchtime,@d2)=0),

week_3=(select (case when (sum(bean) is null) then 0 else sum(bean) end) from match where datediff(week,matchtime,@d3)=0),

week_4=(select (case when (sum(bean) is null) then 0 else sum(bean) end) from match where datediff(week,matchtime,@d4)=0)

where id=9

select * from #temp

第13 24周 統計資訊

歡迎來到效能調優培訓的第4個月。這個月全是關於sql server裡的統計資訊,還有它們如何幫助查詢優化器生成足夠好的執行計畫。統計資訊主要是被查詢優化器用來估計查詢返回的行數。它只是個估計,沒別的。sql server使用在統計資訊物件裡稱作直方圖 histogram 的東西,它描述了對於所給列最...

SQL按照日 周 月 年統計資料

如 表 consume record 字段 consume money型別 date datetime型別 請問怎麼寫四條sql語句分別按日,按周,按月,按季統計消費總量.如 1月 1200元 2月 3400元 3月 2800元 按日 select sum consume day date from...

SQL按照日 周 月 年統計資料

表 consume record 字段 consume money型別 date datetime型別 請問怎麼寫四條sql語句分別按日,按周,按月,按季統計消費總量.如 1月 1200元 2月 3400元 3月 2800元 按日 select sum consume day date from c...