Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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_Oracle - Fatal编程技术网

SQL查询-分组并计算最大-最小值

SQL查询-分组并计算最大-最小值,sql,oracle,Sql,Oracle,需要您帮助在Oracle db中执行SQL查询。当event=Start时,我想将数据划分为多个组。例如,第1-6行是一个组,第7-9行是一个组。我想忽略event=ignore的行。最后,我想计算这些组的maxValue-minValue。我没有任何方法将数据分组 这能实现吗?是否可以按事件=开始使用分区。同样的数据如下: Row Event Value Required Result is max-min of value 1 Start

需要您帮助在Oracle db中执行SQL查询。当event=Start时,我想将数据划分为多个组。例如,第1-6行是一个组,第7-9行是一个组。我想忽略event=ignore的行。最后,我想计算这些组的maxValue-minValue。我没有任何方法将数据分组

这能实现吗?是否可以按事件=开始使用分区。同样的数据如下:

Row Event Value Required Result is max-min of value 1 Start 10 2 A 11 3 B 12 4 C 13 5 D 14 6 E 15 5 -------------------------------------------- 7 Start 16 8 A 18 9 B 20 4 -------------------------------------------- 10 Start 27 11 A 30 12 B 33 13 C 34 7 -------------------------------------------- 14 Ignore 35 -------------------------------------------- 15 Ignore 36 -------------------------------------------- 16 Start 33 17 A 34 18 B 35 19 C 36 20 D 37 21 E 38 5 --------------------------------------------
是的,您可以在SQL中执行此操作

以下查询首先查找行所属的组,方法是查找行id之前的最大起始值。此版本使用相关子查询进行此计算

select groupid, max(value) - min(value)
from (select t.*,
             (select max(row) from t t2 where t2.row < t.row and t2.event = start
             ) as groupid
      from t
     ) t
where event <> 'IGNORE'
然后对id进行分组并进行计算

select groupid, max(value) - min(value)
from (select t.*,
             (select max(row) from t t2 where t2.row < t.row and t2.event = start
             ) as groupid
      from t
     ) t
where event <> 'IGNORE'