根据计算来计算连续行

时间:2019-01-24 12:51:19

标签: tsql sql-server-2014

我需要计算变压器过载的连续小时数。通过将使用量除以尺寸并将其乘以100得到百分比,可以确定变压器是否过载。过载查找表包含用于计算的参数。这需要动态完成,这就是为什么在我需要添加更多参数或更改当前参数的情况下,我不对其进行硬编码的原因。如果变压器在四个小时或更长时间内过载150-174.99%,则将其视为过载。如果变压器在两个小时或更长时间内过载175-199.99%,则将被视为过载,并且如果一个小时或一个小时以上过载200%或更高,则该变压器也将被视为过载。 结果集1 具有几乎所需的输出。请注意第三行的警告。使用同一查询来计算结果集2 ,如果该查询仅在当天和该变压器上运行,则结果集是正确的。当我删除变压器上的where子句并加上日期时,计算就变得很奇怪。我需要能够在大量结果上运行,因此限制结果或创建函数将不起作用。输出都是错误的。

最终,我需要获取此数据,并证明变压器#在一天中[n]小时内过载[n]次。 结果集1 需要显示变压器318过载1次达7个小时。如果从第12小时开始出现另一个过载序列,并持续4个小时,则合计结果应表明变压器过载2次,共11个小时。当我从报表中深入研究此数据时,我想查看下面的结果集。

查询

DROP TABLE #TransformerLoading;
DROP TABLE #OverloadLookup;

CREATE TABLE #TransformerLoading(
TransformerID int,
[Date] date,
Size int,
Usage decimal,
[Hour] int,
OverloadPercent decimal
);
INSERT INTO #TransformerLoading
VALUES
     (318,  '2019-01-11',   5.00,   7.3728, 1,          147.00),
     (318,  '2019-01-11',   5.00,   8.2944, 2,          166.00),
     (318,  '2019-01-11',   5.00,   7.3728, 3,          147.00),
     (318,  '2019-01-11',   5.00,   7.9872, 4,          160.00),
     (318,  '2019-01-11',   5.00,   8.6016, 5,          172.00),
     (318,  '2019-01-11',   5.00,   7.9872, 6,          160.00),
     (318,  '2019-01-11',   5.00,   9.6000, 7,          192.00),
     (318,  '2019-01-11',   5.00,   7.9872, 8,          160.00),
     (318,  '2019-01-11',   5.00,   8.4480, 9,          169.00),
     (318,  '2019-01-11',   5.00,   7.5264, 10,         151.00),
     (318,  '2019-01-11',   5.00,   7.0656, 11,         141.00),
     (318,  '2019-01-11',   5.00,   7.8336, 12,         157.00),
     (318,  '2019-01-11',   5.00,   6.6048, 13,         132.00),
     (318,  '2019-01-11',   5.00,   6.2976, 14,         126.00),
     (318,  '2019-01-11',   5.00,   6.6048, 15,         132.00),
     (318,  '2019-01-11',   5.00,   6.6048, 16,         132.00),
     (318,  '2019-01-11',   5.00,   6.4512, 17,         129.00),
     (318,  '2019-01-11',   5.00,   5.5296, 18,         111.00),
     (318,  '2019-01-11',   5.00,   5.0688, 19,         101.00),
     (318,  '2019-01-11',   5.00,   5.2224, 20,         104.00),
     (318,  '2019-01-11',   5.00,   6.1440, 21,         123.00),
     (318,  '2019-01-11',   5.00,   6.2976, 22,         126.00),
     (318,  '2019-01-11',   5.00,   3.6864, 23,         74.00),
     (318,  '2019-01-11',   5.00,   3.6864, 24,         74.00),
     (3403,   '2019-01-11',  15.00,   146.7000,    1,       978.00),
 (3403,   '2019-01-11',  15.00,   147.1500,    2,       981.00),
 (3403,   '2019-01-11',  15.00,   146.7000,    3,       978.00),
 (3403,   '2019-01-11',  15.00,   147.6000,    4,       984.00),
 (3403,   '2019-01-11',  15.00,   146.7000,    5,       978.00),
 (3403,   '2019-01-11',  15.00,   147.6000,    6,       984.00),
 (3403,   '2019-01-11',  15.00,   147.6000,    7,       984.00),
 (3403,   '2019-01-11',  15.00,   147.6000,    8,       984.00),
 (3403,   '2019-01-11',  15.00,   147.6000,    9,       984.00),
 (3403,   '2019-01-11',  15.00,   147.1500,    10,      981.00),
 (3403,   '2019-01-11',  15.00,   145.8000,    11,      972.00),
 (3403,   '2019-01-11',  15.00,   144.9000,    12,      966.00),
 (3403,   '2019-01-11',  15.00,   145.8000,    13,      972.00),
 (3403,   '2019-01-11',  15.00,   145.3500,    14,      969.00),
 (3403,   '2019-01-11',  15.00,   144.9000,    15,      966.00),
 (3403,   '2019-01-11',  15.00,   126.9000,    16,      846.00),
 (3403,   '2019-01-11',  15.00,   114.7500,    17,      765.00),
 (3403,   '2019-01-11',  15.00,   114.3000,    18,      762.00),
 (3403,   '2019-01-11',  15.00,   115.2000,    19,      768.00),
 (3403,   '2019-01-11',  15.00,   112.9500,    20,      753.00),
 (3403,   '2019-01-11',  15.00,   112.5000,    21,      750.00),
 (3403,   '2019-01-11',  15.00,   112.9500,    22,      753.00),
 (3403,   '2019-01-11',  15.00,   112.9500,    23,      753.00),
 (3403,   '2019-01-11',  15.00,   112.9500,    24,      753.00)
 ;

 CREATE TABLE #OverloadLookup(
 [Hours] int,
 MinPercent decimal,
 MaxPercent decimal
 );

 insert into #OverloadLookup
 VALUES
 (4,150,174.99),
 (2,175,199.99),
 (1,200,99999.99)
 ;

with  a as (
    select *
           ,row_number() OVER (Order by date, [hour]) RowNumber,
           case when [hours] is null then 0 else 1 end IsOverloaded,
           row_number() over (partition by transformerid, date, case when [hours] is null then 0 else 1 end order by date, [hour]) PartitionedRowNumber,
           GroupByRowNumber = row_number() OVER (Order by date, [hour]) - row_number() over (partition by transformerid, date, case when [hours] is null then 0 else 1 end order by date, [hour])
    from #TransformerLoading tl
           left join #OverloadLookup ol on tl.OverloadPercent between ol.MinPercent and ol.MaxPercent
    where tl.Date = '1/11/2019' --and tl.TransformerID = 3403
)
select TransformerID, date, size, usage, hour, OverloadPercent, [Hours], MinPercent, MaxPercent, IsOverloaded,
ROW_NUMBER() OVER (Partition by GroupByRowNumber ORDER BY GroupBYROWNUMBER, [Hour]) ConsecutiveCount
FROM a
order by TransformerID, Date, Hour

OverloadLookup

 Hours   MinPercent     MaxPercent
   4        150           174.99
   2        175           199.99
   1        200         99999.99

结果集1

 TransformerID  date    size    usage   hour    OverloadPercent Hours   MinPercent  MaxPercent  IsOverloaded    ConsecutiveCount
     318    2019-01-11  5.00    7.3728  1           147.00       NULL    NULL          NULL           0                 1
     318    2019-01-11  5.00    8.2944  2           166.00        4       150         174.99          1                 1
     318    2019-01-11  5.00    7.3728  3           147.00       NULL    NULL          NULL           0                 2 (This needs to be one)
     318    2019-01-11  5.00    7.9872  4           160.00        4       150         174.99          1                 1
     318    2019-01-11  5.00    8.6016  5           172.00        4       150         174.99          1                 2
     318    2019-01-11  5.00    7.9872  6           160.00        4       150         174.99          1                 3
     318    2019-01-11  5.00    9.6000  7           192.00        2       175         199.99          1                 4
     318    2019-01-11  5.00    7.9872  8           160.00        4       150         174.99          1                 5
     318    2019-01-11  5.00    8.4480  9           169.00        4       150         174.99          1                 6
     318    2019-01-11  5.00    7.5264  10          151.00        4       150         174.99          1                 7
     318    2019-01-11  5.00    7.0656  11          141.00       NULL    NULL          NULL           0                 1
     318    2019-01-11  5.00    7.8336  12          157.00        4       150         174.99          1                 1
     318    2019-01-11  5.00    6.6048  13          132.00       NULL    NULL          NULL           0                 1
     318    2019-01-11  5.00    6.2976  14          126.00       NULL    NULL          NULL           0                 2
     318    2019-01-11  5.00    6.6048  15          132.00       NULL    NULL          NULL           0                 3
     318    2019-01-11  5.00    6.6048  16          132.00       NULL    NULL          NULL           0                 4
     318    2019-01-11  5.00    6.4512  17          129.00       NULL    NULL          NULL           0                 5
     318    2019-01-11  5.00    5.5296  18          111.00       NULL    NULL          NULL           0                 6
     318    2019-01-11  5.00    5.0688  19          101.00       NULL    NULL          NULL           0                 7
     318    2019-01-11  5.00    5.2224  20          104.00       NULL    NULL          NULL           0                 8
     318    2019-01-11  5.00    6.1440  21          123.00       NULL    NULL          NULL           0                 9
     318    2019-01-11  5.00    6.2976  22          126.00       NULL    NULL          NULL           0                 10
     318    2019-01-11  5.00    3.6864  23          74.00        NULL    NULL          NULL           0                 11
     318    2019-01-11  5.00    3.6864  24          74.00        NULL    NULL          NULL           0                 12

结果集2

 TransformerID  date    size    usage      hour OverloadPercent Hours   MinPercent  MaxPercent  IsOverloaded    ConsecutiveCount         
     3403   2019-01-11  15.00   146.7000    1       978.00        1        200      9999999.00        1                 1
     3403   2019-01-11  15.00   147.1500    2       981.00        1        200      9999999.00        1                 1 (This needs to be two)
     3403   2019-01-11  15.00   146.7000    3       978.00        1        200      9999999.00        1                 1 (This needs to be three)
     3403   2019-01-11  15.00   147.6000    4       984.00        1        200      9999999.00        1                 1 (ect...)
     3403   2019-01-11  15.00   146.7000    5       978.00        1        200      9999999.00        1                 3
     3403   2019-01-11  15.00   147.6000    6       984.00        1        200      9999999.00        1                 1
     3403   2019-01-11  15.00   147.6000    7       984.00        1        200      9999999.00        1                 3
     3403   2019-01-11  15.00   147.6000    8       984.00        1        200      9999999.00        1                 1
     3403   2019-01-11  15.00   147.6000    9       984.00        1        200      9999999.00        1                 3
     3403   2019-01-11  15.00   147.1500    10      981.00        1        200      9999999.00        1                 1
     3403   2019-01-11  15.00   145.8000    11      972.00        1        200      9999999.00        1                 2   
     3403   2019-01-11  15.00   144.9000    12      966.00        1        200      9999999.00        1                 2
     3403   2019-01-11  15.00   145.8000    13      972.00        1        200      9999999.00        1                 1
     3403   2019-01-11  15.00   145.3500    14      969.00        1        200      9999999.00        1                 2
     3403   2019-01-11  15.00   144.9000    15      966.00        1        200      9999999.00        1                 1       
     3403   2019-01-11  15.00   126.9000    16      846.00        1        200      9999999.00        1                 2
     3403   2019-01-11  15.00   114.7500    17      765.00        1        200      9999999.00        1                 1
     3403   2019-01-11  15.00   114.3000    18      762.00        1        200      9999999.00        1                 2
     3403   2019-01-11  15.00   115.2000    19      768.00        1        200      9999999.00        1                 1
     3403   2019-01-11  15.00   112.9500    20      753.00        1        200      9999999.00        1                 2
     3403   2019-01-11  15.00   112.5000    21      750.00        1        200      9999999.00        1                 1
     3403   2019-01-11  15.00   112.9500    22      753.00        1        200      9999999.00        1                 2
     3403   2019-01-11  15.00   112.9500    23      753.00        1        200      9999999.00        1                 1
     3403   2019-01-11  15.00   112.9500    24      753.00        1        200      9999999.00        1                 2

0 个答案:

没有答案