【问题标题】:Weekends between weekdays SQL工作日之间的周末 SQL
【发布时间】:2019-07-01 02:48:14
【问题描述】:

我有一个工作日的日期列表,想要插入周末/公共假期日期的行,并从 SQL 中的前一行填充数据。请帮忙。

【问题讨论】:

  • 欢迎来到 SO。请使用tour 并阅读How to Ask。你有什么尝试?

标签: sql plsql teradata-sql-assistant


【解决方案1】:

您的假期取决于您所在的国家/地区。这是用于在找到星期五后插入两天的逻辑,如果您知道如何识别假期以及假期持续多长时间,则可以应用类似的逻辑。

create table #temp (fechas date, value int)



insert into #temp values ('20160601',2)
insert into #temp values ('20160602',4)
insert into #temp values ('20160603',8)
insert into #temp values ('20160606',2)
insert into #temp values ('20160607',1)


--TABLE
select *, DATEPART(DW,fechas) as dayOfTheWk 
into #temp2 
from #temp

-- selecting Fridays
declare @Fridays TABLE (fechas date,value int, dayOfTheWk int, nRow int)

insert into @Fridays
select *, DATEPART(DW,fechas) as dayOfTheWk, ROW_NUMBER() over(order by fechas) from #temp
where DATEPART(DW,fechas) = 6

declare @i int = 1, @maxI int

select @maxI = count(1) from @Fridays

while(@i <= @maxI)
begin 

        declare @x int = 1

        while (@x <= 2)
        begin 
            insert into #temp2 
            select 
                DATEADD(day,@x,fechas) as fechas, value, DATEPART(DW,DATEADD(day,@x,fechas))
            from @Fridays
            where nRow = @i 

        select @x += 1
        end 

select @i += 1
end 


select * from #temp2

fechas     value    dayOfTheWk
2016-06-01  2        4
2016-06-02  4        5
2016-06-03  8        6
2016-06-04  8        7
2016-06-05  8        1
2016-06-06  2        2
2016-06-07  1        3

【讨论】:

  • 大声笑...可能是这样...但是,金融机构需要跟踪此情况以监控收款周期
猜你喜欢
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多