Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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中查找增加n倍的子序列_Sql_Postgresql_Sequence - Fatal编程技术网

在sql中查找增加n倍的子序列

在sql中查找增加n倍的子序列,sql,postgresql,sequence,Sql,Postgresql,Sequence,接着是我前面的问题,关于在数据集中查找递增子序列 获得结果 x | y -----+----- 94 | 985 469 | 865 525 | 842 610 | 587 765 | 579 从 我可以应用这个查询 select x, y from (select max(x) over (order by y desc) as x_max, x, max(y) over (order by x desc) as y_max, y from table

接着是我前面的问题,关于在数据集中查找递增子序列

获得结果

  x  |  y  
-----+-----
  94 | 985
 469 | 865
 525 | 842
 610 | 587
 765 | 579

我可以应用这个查询

select x, y
from (select max(x) over (order by y desc) as x_max, x, max(y) over (order by x desc) as y_max, y
    from table
    order by y desc, x desc) t
where t.x = t.x_max and t.y = t.y_max
order by y desc, x
现在我的问题是,如何执行n次此操作,即查找x的第2、第3、…、第n递增子序列。 我知道一般的想法是从原始表中获取第一次操作的结果,并对剩余的点执行查询

在我的例子中,在第一次操作之后,我们有剩余的点

  x  |  y  
-----+-----
  73 | 940
 115 | 864
 366 | 862
 448 | 837
 318 | 832
 507 | 826
 244 | 758
 217 | 741
 207 | 732
  54 | 688
 426 | 605
 108 | 604
 142 | 581
 102 | 572
再次应用查询,我们得到

  x  |  y  
-----+-----
  73 | 940
 115 | 864
 366 | 862
 448 | 837
 507 | 826
并对其执行操作

  x  |  y  
-----+-----
 318 | 832
 244 | 758
 217 | 741
 207 | 732
  54 | 688
 426 | 605
 108 | 604
 142 | 581
 102 | 572
诸如此类。我还希望合并这些查询搜索中的所有点,并按y desc对它们进行排序,即

  x  |  y  
-----+-----
  73 | 940
  94 | 985
 115 | 864
 366 | 862
 448 | 837
 469 | 865
 507 | 826
 525 | 842
 610 | 587
 765 | 579

这不是微不足道的&远不是最优的,但您确实可以通过递归CTE实现这一点:

with recursive r as(
  (select   x, y, (running_x_max <> x)::int grp, 0 iteration
   from     (select *, max(x) over (order by y desc) running_x_max
             from   xy) t
   order by 3, 2 desc)
  union all
  (select   x, y, grp + (running_x_max <> x)::int, iteration + 1
   from     (select *, max(x) over (order by y desc) running_x_max
             from   r
             where  grp > iteration) t
   order by 3, 2 desc)
)
select   x, y, grp
from     r
where    grp = iteration
order by 3, 2 desc, 1
递归查询求值?
with recursive r as(
  (select   x, y, (running_x_max <> x)::int grp, 0 iteration
   from     (select *, max(x) over (order by y desc) running_x_max
             from   xy) t
   order by 3, 2 desc)
  union all
  (select   x, y, grp + (running_x_max <> x)::int, iteration + 1
   from     (select *, max(x) over (order by y desc) running_x_max
             from   r
             where  grp > iteration) t
   order by 3, 2 desc)
)
select   x, y, grp
from     r
where    grp = iteration
order by 3, 2 desc, 1