declare @datefrom as datetime,@dateto as datetime

set @datefrom='2015-04-12'
set @dateto='2015-08-13' declare @table as table(dweek int,fdate datetime,tdate datetime)
declare @yearfrom as int,@monthfrom as int,@dayfrom as int
declare @yearto as int,@monthto as int,@dayto as int
set @yearfrom=year(@datefrom)
set @monthfrom=month(@datefrom)
set @dayfrom=day(@datefrom)
set @yearto=year(@dateto)
set @monthto=month(@dateto)
set @dayto=day(@dateto) declare @flag as int
set @flag=1 if(@flag=1)
begin
declare @firstDay as datetime, @currentDay as datetime,@monthdays as int
declare @curyear as int,@curmonth as int,@curday as int,@dweek as int,@firstflag int
set @curyear=@yearfrom
set @curmonth=@monthfrom
set @curday=@dayfrom set @firstflag=0
set @currentDay= str(@curyear) +'-'+str(@curmonth) +'-'+str(@curday)
set @dweek=1
while(@dateto>=@currentDay)
begin
set @firstDay= str(@curyear) +'-'+str(@curmonth) +'-01'
set @monthdays= day(dateadd(d,-day(@firstDay),dateadd(m,1,@firstDay)))
while(@monthdays>=@curday )
begin
set @currentDay= str(@curyear) +'-'+str(@curmonth) +'-'+str(@curday)
set @curday=@curday+1 if datepart(weekday,@currentDay)=1 and @dateto>=@currentDay
begin
if(@firstflag=1)
insert into @table(dweek,fdate,tdate)
select @dweek,dateadd(day,-6,@currentDay),@currentDay
else
insert into @table(dweek,fdate,tdate)
select @dweek,@datefrom,@currentDay set @dweek=@dweek+1
set @firstflag=1
end
end
set @curday=1
if(@curmonth=12)
begin
set @curmonth=1
set @curyear=@curyear+1
end
else
begin
set @curmonth=@curmonth+1
end
end
end if(datepart(weekday,@dateto)>1)
begin
insert into @table(dweek,fdate,tdate)
select @dweek,dateadd(day,2-datepart(weekday,@dateto),@dateto),@dateto
end select *from @table

统计时间段内周分类SQL语句的更多相关文章

  1. 使用mysqlbinlog从二进制日志文件中查询mysql执行过的sql语句 (原)

    前提MySQL开启了binlog日志操作1. 查看MySQL是否开启binlog(进mysql操作) mysql> show variables like 'log_bin%';       2 ...

  2. MySQL注入点与SQL语句的关系

    目录 注入位置分类 内联式 - UNION query SQL injection 终止式 - End SQL injection 堆叠式 - Stacked queries SQL injectio ...

  3. sql server 查询性能最差的sql语句

    SELECT TOP 10 TEXT AS 'SQL Statement' ,last_execution_time AS 'Last Execution Time' ,(total_logical_ ...

  4. mysql执行sql语句过程

    开发人员基本都知道,我们的数据存在数据库中(目前最多的是mysql和oracle,由于作者更擅长mysql,所以这里默认数据库为mysql),服务器通过sql语句将查询数据的请求传入到mysql数据库 ...

  5. sql语句执行的时间

    统计mysql里每条SQL语句执行的时间 收藏 CrazyHarry 发表于 2年前 阅读 3785 收藏 8 点赞 3 评论 3 Google.Github 双重认证前端课程,独家硅谷内容,每周直播 ...

  6. mysql按年度、季度、月度、周、日统计查询的sql语句

    本文介绍一些mysql中用于查询的sql语句,包括按年度.季度.月度.周.日统计查询等,有需要的朋友,可以参考下. 一.年度查询 查询 本年度的数据   SELECT * FROM blog_arti ...

  7. 数据库:sql语句分别按日,按周,按月,按季统计金额

    如: 表:consume_record 字段:consume (money类型) date (datetime类型) 请问怎么写四条sql语句分别按日,按周,按月,按季统计消费总量. 如:1月 120 ...

  8. vertica时间计算SQL语句实例:统计一天内登录的用户

    SQL语句实例: select count(id) as num from public.user where cast((CURRENT_TIMESTAMP-login_timed) day as ...

  9. 数据库SQL语句查询指定时间段内的数据

    [摘要]有的时候,我们需要查询数据库某段时间之间的数据,比如2016年5月1号到到5月3号之间用户注册数量(特殊节假日期间)等.那么用SQL语句如何实现呢? 首先,数据表中的存时间的字段比如是addt ...

随机推荐

  1. mysql中日期比较大小的方法

    假如有个表product有个字段add_time,它的数据类型为datetime,有人可能会这样写sql: select * from product where add_time = '2013-0 ...

  2. IOS发送Email的两种方法

    IOS系统框架提供的两种发送Email的方法:openURL 和 MFMailComposeViewController.借助这两个方法,我们可以轻松的在应用里加入如用户反馈这类需要发送邮件的功能. ...

  3. Switching regulator forms constant-current source

    Many applications require current sources rather than voltage sources. When you need a high-current ...

  4. RTL8188EUS带天线的WiFi模块

    http://www.liuliutech.com/ProductShow.asp?ID=121 一,公司介绍瑞昱(REALTEK)半导体成立于1987年,位于台湾[硅谷]的新竹科学园区.凭借着7位创 ...

  5. 字符集UTFMB4

    http://my.oschina.net/leejun2005/blog/232732#OSC_h3_4 http://ju.outofmemory.cn/entry/211473

  6. A电脑的gho还原到B电脑上的驱动解决方案

    近来给B笔记本做系统,我有一个gho,是A电脑的,我想直接把系统复制过去,我的这个gho有50G,里面已经配置好了java,安卓,cocos2dx的各种环境变量,安卓开发的朋友都知道这个有多恶心,我就 ...

  7. 高级需求分析UML建模设计模式笔记

    1.REQ->HLR 分析 全系统性质->AD设计 Context,BOM,Conception 2.REQ->LLR 分析 模块分析->DD设计 + 编码 Feature,B ...

  8. jQuery -> filter使用方法

    利用filter函数能够从wrapper set中过滤符合条件的DOM元素. 假设我们有一个内容例如以下的html文件,要获取类为external的<a>元素,使用filter能够非常ea ...

  9. thymleaf 常用th 标签

    常用th标签都有那些? 关键字 功能介绍 案例 th:id 替换id <input th:id="'xxx' + ${collect.id}"/> th:text 文本 ...

  10. 总结学习 Python 的 14 张思维导图汇总