统计时间段内周分类SQL语句

简介:
复制代码
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
复制代码
相关文章
|
1月前
|
SQL Java 数据库连接
如何在 Java 代码中使用 JSqlParser 解析复杂的 SQL 语句?
大家好,我是 V 哥。JSqlParser 是一个用于解析 SQL 语句的 Java 库,可将 SQL 解析为 Java 对象树,支持多种 SQL 类型(如 `SELECT`、`INSERT` 等)。它适用于 SQL 分析、修改、生成和验证等场景。通过 Maven 或 Gradle 安装后,可以方便地在 Java 代码中使用。
334 11
|
8月前
|
SQL 关系型数据库 MySQL
MySQL数据库基础第一篇(SQL通用语法与分类)
MySQL数据库基础第一篇(SQL通用语法与分类)
|
3月前
|
SQL
开启慢SQL设置long_query_time=0.1为啥会统计的sql却存在小于100毫秒的sql
开启慢SQL设置long_query_time=0.1为啥会统计的sql却存在小于100毫秒的sql
66 1
|
4月前
|
SQL 监控 安全
SQL注入公鸡分类及原理
SQL注入公鸡分类及原理
|
4月前
|
SQL 存储 关系型数据库
mysql 数据库空间统计sql
mysql 数据库空间统计sql
66 0
|
7月前
|
SQL 存储 关系型数据库
SQL分类与数据类型
【7月更文挑战第12天】Mysql SQL语句分类与数据类型 介绍
|
6月前
|
SQL 存储 关系型数据库
SQL SERVER 查询所有表 统计每张表的大小
SQL SERVER 查询所有表 统计每张表的大小
66 0
|
8月前
|
SQL 存储 关系型数据库
MySQL数据库——SQL(1)-SQL通用语法、SQL分类、DDL(数据库操作、表操作)
MySQL数据库——SQL(1)-SQL通用语法、SQL分类、DDL(数据库操作、表操作)
101 1
|
9月前
|
SQL BI HIVE
【Hive SQL 每日一题】统计用户留存率
用户留存率是衡量产品成功的关键指标,表示用户在特定时间内持续使用产品的比例。计算公式为留存用户数除以初始用户数。例如,游戏发行后第一天有10000玩家,第七天剩5000人,第一周留存率为50%。提供的SQL代码展示了如何根据用户活动数据统计每天的留存率。需求包括计算系统上线后的每日留存率,以及从第一天开始的累计N日留存率。通过窗口函数`LAG`和`COUNT(DISTINCT user_id)`,可以有效地分析用户留存趋势。
559 1
|
8月前
|
SQL 关系型数据库 MySQL

热门文章

最新文章