ID AnswerDay advId
1 2007-12-30 14:38:56 22
2 2008-1-1 14:38:56 22
3 2008-1-2 14:38:56 38
4 2008-1-3 14:38:56 22
5 2008-3-1 14:38:56 22
6 2008-3-3 14:38:56 22
7 2008-5-1 14:38:56 22
8 2008-5-2 14:38:56 38
9 2008-7-1 14:38:56 22
10 2008-7-3 14:38:56 22
11 2008-7-31 14:38:56 38------------------------------------------------- 我现在想得到:TheTime CountNum
2008年1月 2
2008年2月 0
2008年3月 2
2008年4月 0
2008年5月 1
2008年6月 0
2008年7月 2
2008年8月 0
......
2008年12月 0
2 2008-1-1 14:38:56 22
3 2008-1-2 14:38:56 38
4 2008-1-3 14:38:56 22
TheTime CountNum
2008年1月 2怎么理解
1 2007-12-30 14:38:56 22
2 2008-1-1 14:38:56 22
3 2008-1-2 14:38:56 38
4 2008-1-3 14:38:56 22
5 2008-3-1 14:38:56 22
6 2008-3-3 14:38:56 22
7 2008-5-1 14:38:56 22
8 2008-5-2 14:38:56 38
9 2008-7-1 14:38:56 22
10 2008-7-3 14:38:56 22
11 2008-7-31 14:38:56 38------------------------------------------------- 我现在想得到:TheTime CountNum
2008年1月 2
2008年2月 0
2008年3月 2
2008年4月 0
2008年5月 1
2008年6月 0
2008年7月 2
2008年8月 0
......
2008年12月 0
2 2008-1-1 14:38:56 22
3 2008-1-2 14:38:56 38
4 2008-1-3 14:38:56 22
TheTime CountNum
2008年1月 2怎么理解
应该用 rtrim(month([AnswerDay]))
insert into @t
select 1, '2007-12-30 14:38:56', 22 union
select 2, '2008-1-1 14:38:56', 22 union
select 3, '2008-1-2 14:38:56', 38 union
select 4, '2008-1-3 14:38:56', 22 union
select 5, '2008-3-1 14:38:56', 22 union
select 6, '2008-3-3 14:38:56', 22 union
select 7, '2008-5-1 14:38:56', 22 union
select 8, '2008-5-2 14:38:56', 38 union
select 9, '2008-7-1 14:38:56', 22 union
select 10, '2008-7-3 14:38:56', 22 union
select 11, '2008-7-31 14:38:56', 38 select '2008年'+cast(rn as varchar(2))+'月' as thetime,count(distinct advid) as countnum
from (select top 12 (select count(*) from sysobjects where id<=a.id) as rn from sysobjects a) b
left join (select * from @t where datepart(yy,AnswerDay)='2008') c on b.rn = datepart(mm,c.AnswerDay)
group by rn
-- Author:Flystone
-- Version:V1.001
-- Date:2008-08-01 10:25:02
-------------------------------------- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(ID int,AnswerDay datetime,advId int)
Go
Insert into ta
select 1,'2007-12-30 14:38:56',22 union all
select 2,'2008-1-1 14:38:56',22 union all
select 3,'2008-1-2 14:38:56',38 union all
select 4,'2008-1-3 14:38:56',22 union all
select 5,'2008-3-1 14:38:56',22 union all
select 6,'2008-3-3 14:38:56',22 union all
select 7,'2008-5-1 14:38:56',22 union all
select 8,'2008-5-2 14:38:56',38 union all
select 9,'2008-7-1 14:38:56',22 union all
select 10,'2008-7-3 14:38:56',22 union all
select 11,'2008-7-31 14:38:56',38
Go
--Start
Select b.id,count(a.advid) as cnt
from ta a right join(select 1 as id union select 2 union select 3
union select 4 as id union select 5 union select 6
union select 7 as id union select 8 union select 9
union select 10 as id union select 11 union select 12) b
on datepart(mm,a.answerday) = b.id
and advid = 22 and datepart(yy,answerday) = 2008
group by b.id
--Result:
/*
id cnt
----------- -----------
1 2
2 0
3 2
4 0
5 1
6 0
7 2
8 0
9 0
10 0
11 0
12 0*/
--End
-- Author:Flystone
-- Version:V1.001
-- Date:2008-08-01 10:25:02
-------------------------------------- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(ID int,AnswerDay datetime,advId int)
Go
Insert into ta
select 1,'2007-12-30 14:38:56',22 union all
select 2,'2008-1-1 14:38:56',22 union all
select 3,'2008-1-2 14:38:56',38 union all
select 4,'2008-1-3 14:38:56',22 union all
select 5,'2008-3-1 14:38:56',22 union all
select 6,'2008-3-3 14:38:56',22 union all
select 7,'2008-5-1 14:38:56',22 union all
select 8,'2008-5-2 14:38:56',38 union all
select 9,'2008-7-1 14:38:56',22 union all
select 10,'2008-7-3 14:38:56',22 union all
select 11,'2008-7-31 14:38:56',38
Go
--Start
Select cast('2008年'+ltrim(b.id)+'月' as varchar) as TheTime,count(a.advid) as cnt
from ta a right join(select 1 as id union select 2 union select 3
union select 4 as id union select 5 union select 6
union select 7 as id union select 8 union select 9
union select 10 as id union select 11 union select 12) b
on datepart(mm,a.answerday) = b.id
and advid = 22 and datepart(yy,answerday) = 2008
group by b.id
--Result:
/*
TheTime cnt
------------------------------ -----------
2008年1月 2
2008年2月 0
2008年3月 2
2008年4月 0
2008年5月 1
2008年6月 0
2008年7月 2
2008年8月 0
2008年9月 0
2008年10月 0
2008年11月 0
2008年12月 0*/
--End
SELECT TOP 12 IDENTITY(INT) theID INTO # FROM sysobjectsselect c.thetime,isnull(d.cnt,0) from
(SELECT DISTINCT ltrim(str(year(TableOne.AnswerDay)))+'年'+right('0'+ltrim(str(#.theID)),2)+'月' as thetime
FROM TableOne,#) c left join(select b.thetime,count(b.thetime) as cnt from(SELECT DATENAME(year,[AnswerDay]) + '年' + DATENAME(month,[AnswerDay])+ '月' as TheTime
FROM TableOne) b group by b.thetime) don c.thetime=d.thetime(12 行受影响)
thetime
------------------ -----------
2008年01月 2
2008年02月 0
2008年03月 1
2008年04月 2
2008年05月 0
2008年06月 1
2008年07月 1
2008年08月 1
2008年09月 0
2008年10月 0
2008年11月 0
2008年12月 0(12 行受影响)
SELECT TheTime,COUNT(AnswerDay) as CountNum
FROM (SELECT DISTINCT DATENAME(year,[AnswerDay])+'年'+ RTRIM(theID)+'月'as TheTime,theid
FROM getAdverStatistics
CROSS JOIN #
) a
LEFT JOIN getAdverStatistics b
on b.advId=22 and DATENAME(year,[AnswerDay])+'年' + convert(varchar,datepart(month,[AnswerDay]))+'月'=TheTime
where left(TheTime,4)=datename(yy,getdate())
group by TheTime,theid
order by theid
set @year='2008'
select rtrim(@year)+'年'+rtrim(A.月)+'月' 年月,isnull(数,0)数量 from
(select top 12 langid+1 月 from master..syslanguages order by langid)A
left join (select year(AnswerDay)年,month(AnswerDay)月,count(*)数 from getAdverStatistics
where advId=22 and year(AnswerDay)=2008 group by year(AnswerDay),month(AnswerDay)
)B on A.月=B.月
----如果是按年查也可以这样
2008年1月 2
2008年2月 0
2008年3月 2
2008年4月 0
2008年5月 1
2008年6月 0
2008年7月 2
2008年8月 0
2008年9月 0
2008年10月 0
2008年11月 0
2008年12月 0
TheTime CountNum
---------------------------------------------- -----------
2008年1月 2
2008年2月 0
2008年3月 2
2008年4月 0
2008年5月 1
2008年6月 0
2008年7月 2
2008年8月 0
2008年9月 0
2008年10月 0
2008年11月 0
2008年12月 0(所影响的行数为 12 行)*/
可以实现的吧 order by month(TheTime)因为month(TheTime)是int型的,不会出现11,12排到2前面的问题吧
declare @year varchar(4)
set @year='2008'
-----------------------------------
select @year+'年'+rtrim(A.月)+'月' 年月,isnull(数,0)数量 from
(select top 12 langid+1 月 from master..syslanguages order by langid)A
left join (select month(AnswerDay)月,count(*)数 from getAdverStatistics
where advId=22 and year(AnswerDay)=@year group by month(AnswerDay)
)B on A.月=B.月
thetime是字符串类型的 不能用month()
month(Convert(datetime,TheTime,120))