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
    希望可以帮到有需要的朋友,如果没有需要也可以留着备用哦,嘻嘻~

  • 相关阅读:
    Transform XML using XSLT
    HyperV Remote Management Configuration utility
    UI testing via Reflection in .NET
    Auto Test: Test Case Structure
    UI Testing via windows API
    风讯CMS常见问题锦集
    深入了解ASP.NET运行内幕 .txt
    公布一个简单的日志记录方法 【转】要研究
    服务器asp.net权限设置问题及解决方法时间:
    C#如何去掉字符串中所有空格
  • 原文地址:https://www.cnblogs.com/medci/p/1574501.html
Copyright © 2011-2022 走看看