日期:2014-05-17 浏览次数:20833 次
select RIGHT( convert(varchar(10),updateTime,120),2) as 'dd', sum(voteNum) as 'vnum',count(distinct uid) as 'voteusernum' ,convert(nvarchar(10),updateTime,120) as 'udtime' from r_vipvote_uid where year(updateTime)=year('2012-10-01') and month(updateTime)=month('2012-10-01') group by convert(nvarchar(10),updateTime,120),RIGHT( convert(varchar(10),updateTime,120),2)
SELECT RIGHT(a.dt,2) AS dt,vnum,voteusernum FROM ( SELECT CONVERT(NVARCHAR(10),DATEADD(dd,number,DATEADD(mm,DATEDIFF(mm,0,'2012-10-01'),0)),120) AS dt FROM MASTER..spt_values AS s WHERE type='p' AND number BETWEEN 0 AND DATEDIFF(dd,'2012-10-01',DATEADD(mm,1,'2012-10-01')-1) ) a left JOIN ( SELECT SUM(voteNum) AS 'vnum' , COUNT(DISTINCT uid) AS 'voteusernum' , CONVERT(NVARCHAR(10) , updateTime , 120) AS 'udtime' FROM r_vipvote_uid WHERE YEAR(updateTime) = YEAR('2012-10-01') AND MONTH(updateTime) = MONTH('2012-10-01') GROUP BY CONVERT(NVARCHAR(10) , updateTime , 120) ) b ON a.dt=b.udtime
------解决方案--------------------
create table #r_vipvote_uid (id int,uid int,writingid int,updateTime datetime,ip varchar(30),votenum int,fuid int,votecount int) select * from #r_vipvote_uid insert into #r_vipvote_uid select 1,7,4932,'2011-02-03 00:24:16.597','113.109.221.49',1,7,0 union all select 2,1237,3492,'2011-02-04 00:26:05.597','113.129.221.49',1,1237,0 union all select 3,237,3289,'2011-09-05 00:27:12.597','113.139.221.49',1,237,0 union all select 4,347,2292,'2011-09-08 00:28:18.597','113.149.221.49',1,347,0 union all select 5,37,3392,'2011-09-10 00:29:16.597','113.159.221.49',1,37,0 union all select 6,987,1942,'2011-09-12 00:30:16.597','113.169.221.49',1,987,0 union all select 7,4567,2954,'2011-09-25 00:31:16.597','113.189.221.49',1,4567,0 union all select 8,3457,3921,'2011-09-26 00:31:30.597','113.199.221.49',1,3457,0 union all select 9,7567,2492,'2011-09-27 00:45:28.597','113.129.221.149',1,7567,0 ;with cte_date as ( select distinct cast(left(convert(nvarchar(10),updateTime,120),8)+'01'as datetime) as year_month from #r_vipvote_uid -- where year(updateTime)=year('2012-10-01') and month(updateTime)=month('2012-10-01') ) ,cte_alldate as ( select convert(varchar(30),dateadd(day,b.number-1,year_month),120) as date from cte_date a inner join master.dbo.spt_values b on b.type='p' and b.number between 1 and datediff(day,year_month,dateadd(month,1,year_month)) ) select RIGHT( convert(varchar(10),a.date,120),2) as 'dd' ,sum(voteNum) as 'vnum' ,count(distinct uid) as 'voteusernum' ,convert(nvarchar(10),a.date,120) as 'udtime' from cte_alldate a left join #r_vipvote_uid b on convert(nvarchar(10),a.date,120)=convert(nvarchar(10),b.updateTime,120) group by convert(nvarchar(10),a.date,120),RIGHT( convert(varchar(10),a.date,120),2) order by udtime
------解决方案--------------------
year(updateTime)=year('2012-10-01') and month(updateTime)=month('2012-10-01')
------解决方案--------------------