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. (转)Ext.Button点击事件的三种写法

    转自:http://maidini.blog.163.com/blog/static/377627042008111061844345/ ExtJs的写法太灵活了,现在收集了关于Button点击事件的 ...

  2. Scrapy 轻松定制网络爬虫(转)

    网络爬虫(Web Crawler, Spider)就是一个在网络上乱爬的机器人.当然它通常并不是一个实体的机器人,因为网络本身也是虚拟的东西,所以这个“机器人”其实也就是一段程序,并且它也不是乱爬,而 ...

  3. asp.net Core 中间件Hello world

    虽然在ASP.NET 5中,微软没有再强调OWIN(Open Web Interface for .NET)及其微软官方的OWIN实现Katana,但是其中涉及到一些原则和设计思想依然被ASP.NET ...

  4. golang的缓冲channel和无缓冲channel的区别

    话说golang的channel同步的定义真是让人无力吐槽,码农的用户体验就这么难搞么,超耐磨阿,无缓冲和缓冲居然有这么大区别....靠 转载一段网上的资料 --------------------- ...

  5. easyui datagrid checkbox multiple columns have been done do

    lengku1987   2013-01-06 22:27:47   Sponsored Links   easyui datagrid checkbox multiple columns have ...

  6. go语言基础之递归实现数字累加

    1.实现1+100 = 5050 示例: package main import "fmt" //实现1+2+3+……100 func test01() (sum int) { f ...

  7. 图片变换【Matrix】矩阵 简介

    Matrix矩阵介绍 官方文档地址:https://developer.android.com/reference/android/graphics/Matrix.html 在Android中,对图片 ...

  8. Node.js:常用工具、路由

    一.常用工具util util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. 1.util.inherits util.inherit ...

  9. ShareSDK for Android 2.3.10已经公布

    ShareSDK for Android 2.3.10已经公布,本次更新内容包含: 1.加入自己定义分享标签功能 新版本号SDK下载页面地址: http://share.sharesdk.cn/Dow ...

  10. linux下运行telnet命令出现command not find解决办法

    原因是没有安装telnet客户端和服务(缺一不可) yum list telnet*   查看telnet相关的安装包yum install telnet-server 安装telnet服务yum i ...