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. Vue学习记录-状态管理

    要解决的问题 平时的系统开发中,基本都会碰到这个权限问题,需要根据用户的登录状态进行处理.最常见的情况就是“先登录,后使用”.除去打包成APP,无法看到连接外,如果地址栏里直接输入地址就能绕过登录的话 ...

  2. Java容器-引入Guava类库

    目录 1.只读设置 2.函数式编程+组合式编程 3.约束条件 4.集合操作(并集.差集.交集) 代码实现 1.只读设置 public static void main(String [] args){ ...

  3. 王者参考jar包

  4. hdu 2176 取石子游戏

    http://acm.hdu.edu.cn/showproblem.php?pid=2176 提示:尼姆博弈,异或 #include <iostream> #include <cst ...

  5. mysql配置文件my.ini优化详解

    mysql 5.5.13参数说明:[client]character-set-server = utf8port = 3306socket = /data/mysql/3306/mysql.sock[ ...

  6. VS2010程序打包操作(超详细的)转

    1.  在vs2010 选择“新建项目”----“其他项目类型”----“Visual Studio Installerà“安装项目”: 命名为:Setup1 . 这是在VS2010中将有三个文件夹, ...

  7. android——根据MVC框架设计的结构

  8. SWT常用组件

    SWT类所代表的事件常量: 事件类型常量 说明 SWT.Activate 当激活窗口时 SWT.Arm 菜单项被选中之前 SWT.Close 关闭窗口时 SWT.Collapse 折叠树的节点时 SW ...

  9. [Android 新特性] Android 4.3 Top 5新功能

    直播会上,Android 4.3不像Nexus 7那么Sexy,也没有Chromecast电视棒那样惹人注目,但它有许多强悍的新特点,下面来看看Top 5. 1. 权限配置 支持在一台终端设备上有多个 ...

  10. Spark源码分析:多种部署方式之间的区别与联系(转)

    原文链接:Spark源码分析:多种部署方式之间的区别与联系(1) 从官方的文档我们可以知道,Spark的部署方式有很多种:local.Standalone.Mesos.YARN.....不同部署方式的 ...