(
select
ROW_NUMBER() OVER (ORDER BY substring(convert(varchar,dateadd(day,x,col),120),1,10)) AS rowid,substring(convert(varchar,dateadd(day,x,col),120),1,10) AS 日期,'最后一天' AS 标识 from
(
select cast('2010-1-1' as datetime) as col
)a cross join
(
select top 365 b8.i+b7.i + b6.i + b5.i + b4.i +b3.i +b2.i + b1.i + b0.i x
from(select 0 i union all select 1) b0
cross join(select 0 i union all select 2) b1
cross join(select 0 i union all select 4) b2
cross join(select 0 i union all select 8) b3
cross join(select 0 i union all select 16) b4
cross join(select 0 i union all select 32) b5
cross join(select 0 i union all select 64) b6
cross join(select 0 i union all select 128) b7
cross join(select 0 i union all select 256) b8
order by 1
)b
where datepart(dw,dateadd(day,x,col))=6
) aa
WHERE rowid=3
/*
说明 日期
----------------- --------------------
第3周最周一天 2010-01-15
*/
假设members 表里有个字段是日期(CDATA),和一个ID字段,要统计每天的条数,可以用下面的语句:SELECT i.CDATA,count(*) from members i
where cdata>=date_sub(now(),interval dayofweek(now()) day) and cdata <= date_add(now(),interval 7-dayofweek(now()) day) group by i.CDATA
以上语句统计的是从这周的第一天(星期天)到这周的最后一天(星期六)每天的记录条数。
程序里取出来循环得到每天的条数(查询出来的结果已经按日期分好,只要读取就行)
select convert(varchar(10),getdate()-(datepart(weekday,getdate())-2),120)as "第一天(周一)",convert(varchar(10),getdate()+(8-datepart(weekday,getdate())),120)as "最后一天(周日)"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)