Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于对连续行进行分组的SQL函数_Sql_Tsql - Fatal编程技术网

用于对连续行进行分组的SQL函数

用于对连续行进行分组的SQL函数,sql,tsql,Sql,Tsql,我有一个案例最初是在excel中完成的,我正试图在查询中复制它。我的数据结构如下: Field1 | Field2 --------------- 1 |5 2 |10 3 |5 4 |10 5 |5 我需要一个可以按/agg按连续3行分组的函数,以便返回: Field1 | Field2 --------------- 123 |20 234 |25 345 |20 使用LEAD功能,如中所示: with x as (

我有一个案例最初是在excel中完成的,我正试图在查询中复制它。我的数据结构如下:

Field1 | Field2
---------------
1      |5
2      |10
3      |5
4      |10
5      |5
我需要一个可以按/agg按连续3行分组的函数,以便返回:

Field1 | Field2
---------------
123    |20
234    |25
345    |20
使用LEAD功能,如中所示:

with
x as (
  select
    field1 as x1,
    lead(field1) as x2,
    lead(field1, 2) as x3,
    field2 as y1,
    lead(field2) as y2,
    lead(field2, 2) as y3
  from my_table
)
select
  x1 + x2 + x3 as field1,
  y1 + y2 + y3 as field2
from x
where x1 is not null
  and x2 is not null
  and x3 is not null