标签:
--统计某月份周情况 没有的补0
if object_id(‘temp_tb‘) is not null
drop table temp_tb
create table temp_tb
([ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[test_values] int NULL,
[time] datetime null,
);
go
insert into temp_tb([test_values],[time])
select 3,‘2009-2-1 10:12:30‘ union all
select 5,‘2009-2-5 09:20:23‘ union all
select 6,‘2009-2-6 11:21:34‘ union all
select 7,‘2009-2-8 12:22:12‘ union all
select 2,‘2009-2-10 16:45:25‘ union all
select 3,‘2009-2-13 13:21:14‘ union all
select 5,‘2009-2-15 15:58:09‘ union all
select 1,‘2009-2-15 08:35:47‘ union all
select 1,‘2009-2-15 09:13:07‘ union all
select 9,‘2009-2-15 09:15:04‘
select * from temp_tb
SELECT
A.[Week],
ISNULL(COUNT(B.test_values),0) AS num
FROM (
SELECT
datepart(week,dateadd(week,0,dateadd(day,number,‘2009-02-01‘))) AS [Week]
FROM master.dbo.spt_values
WHERE type=‘p‘ AND