Advertisement
Guest User

Bidrag 2

a guest
Mar 23rd, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.30 KB | None | 0 0
  1. create function dbo.GetNums(@N as bigint) returns table
  2. as
  3. return
  4.   with
  5.   L0 as (select 1 as C union all select 1),
  6.   L1 as (select 1 as C from l0 as A cross join l0 as B),
  7.   L2 as (select 1 as C from l1 as A cross join l1 as B),
  8.   L3 as (select 1 as C from l2 as A cross join l2 as B),
  9.   L4 as (select 1 as C from l3 as A cross join l3 as B),
  10.   L5 as (select 1 as C from l4 as A cross join l4 as B)
  11.   select top (@N) row_number() over(order by (select null)) as N
  12.   from (
  13.        select 1 as C
  14.        from (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) as T10(C)
  15.        union all
  16.        select 1 as C
  17.        from L5
  18.        ) as N
  19.   order by C;
  20.  
  21. go
  22.  
  23. create table dbo.CS(CS bit not null, index IX_CS clustered columnstore);
  24.  
  25. go
  26.  
  27. declare @N int = 2147483647;
  28.  
  29.  
  30. with P as
  31. (
  32.   select V.AssemblyLine,
  33.          V.MinLogTime,
  34.          P.Interval
  35.   from (
  36.        select V.AssemblyLine,
  37.               cast(cast(min(V.LogTime) as date) as datetime2(0)) as MinLogTime,
  38.               max(V.LogTime) as MaxLogTime,
  39.               count_big(*) as LogItemCount
  40.        from dbo.[Values] as V
  41.        group by V.AssemblyLine
  42.        )
  43.        as V
  44.     inner join dbo.Parameters as P
  45.       on V.AssemblyLine = P.AssemblyLine
  46.     where V.LogItemCount < 1 + (datediff_big(minute, V.MinLogTime, V.MaxLogTime) / P.Interval)
  47. ),
  48. C1 as
  49. (
  50.   select V.AssemblyLine,
  51.          datediff_big(minute, P.MinLogTime, V.LogTime) / P.Interval as LogTimeSlot,
  52.          P.MinLogTime,
  53.          P.Interval
  54.   from dbo.[Values] as V
  55.     inner join P
  56.       on V.AssemblyLine = P.AssemblyLine
  57. ),
  58. C2 as
  59. (
  60.   select C1.AssemblyLine,
  61.          C1.Interval,
  62.          C1.LogTimeSlot,
  63.          lead(C1.LogTimeSlot) over(partition by C1.AssemblyLine order by C1.LogTimeSlot) as LeadLogTimeSlot,
  64.          C1.MinLogTime
  65.   from C1
  66. )
  67. insert into dbo.[Values] with(tablock) (AssemblyLine, LogTime)
  68. select C2.AssemblyLine,
  69.        dateadd(day, N.MinToAdd / 1440, dateadd(minute, N.MinToAdd % 1440, C2.MinLogTime))
  70. from C2
  71.   cross apply (
  72.               select top(@N) C2.Interval * (N.N + C2.LogTimeSlot) as MinToAdd
  73.               from dbo.GetNums(C2.LeadLogTimeSlot - C2.LogTimeSlot - 1) as N
  74.                     ) as N
  75.   left outer join dbo.CS on 0 = 1
  76. where C2.LeadLogTimeSlot - C2.LogTimeSlot > 1
  77. option(recompile, optimize for (@N = 1));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement