zoukankan      html  css  js  c++  java
  • 将数据平均分配到一个时间段内,周末去掉(精典)

    最近写了一段SQL,因为时间跳变的原因,需要将一些数据平均分配到一个日期段内,但是周末要除外,在此PO出来,希望有需要的朋友可以瞄两眼,呵~~~~

    declare @datetime datetime
    declare @datetimey datetime
    declare @termrecordid int
    set @datetime='2009-07-09'   --此处设定的是一个初使的值,后面会根据情况进行累加
    declare update_cursor cursor for

    select termrecordid,consumedate from te_consumedetailxf where termid=229 and
    convert(char(10),consumedate,21) between '2005-01-01' and '2005-12-01'
    and consumedetailid between '134900' and '170987' order by termrecordid

    open update_cursor
    fetch next from update_cursor
    into @termrecordid,@datetimey

    while @@FETCH_STATUS = 0
    begin

    if (@termrecordid%14 = 0)    --一天插入14条,满14条时,日期要向前加一
    begin
    set @datetime=dateadd(day,1,@datetime)   
    SET DATEFIRST 1
    select DATEPART(WEEKDAY,@datetime)   
    select @datetime=
           case DATEPART(WEEKDAY,'2009-02-01') when 6 then dateadd(day,2,@datetime)
           when 7 then dateadd(day,1,@datetime)  else @datetime end                     
    end     --此处是判断是否为周末,若为周6则再加一,若为周日则再加二
     

    update te_consumedetailxf
    set consumedate=convert(datetime,@datetime+''+convert(varchar(10),@datetimey,114))
    where termid=229 and
    (convert(char(10),consumedate,21) between '2005-01-01' and '2005-12-01' )
    and (consumedetailid between '134900' and '170987') and termrecordid=@termrecordid

    fetch next from update_cursor into
    @termrecordid,@datetimey

    end

    fetch next from update_cursor

    close update_cursor
    deallocate update_cursor
    希望可以帮到有需要的朋友,如果没有需要也可以留着备用哦,嘻嘻~

  • 相关阅读:
    编译nginx增加fair模块
    使用CentOS8来部署php7.4
    通过PHP代码将大量数据插入到Sqlite3
    不同程序语言处理加密算法的性能对比(PHP/Golang/NodeJS)
    CentOS8更换国内YUM源
    MySQL获取上月第一天、上月最后日、本月第一天、本月最后日的方法
    GO
    Go-数据类型以及变量,常量,函数,包的使用
    GO语言介绍以及开发环境配置
    利用python代码操作git
  • 原文地址:https://www.cnblogs.com/medci/p/1574501.html
Copyright © 2011-2022 走看看