7

假设我有这张桌子:

select * from window_test;

 k | v
---+---
 a | 1
 a | 2
 b | 3
 a | 4

最终我想得到:

 k | min_v | max_v
---+-------+-------
 a | 1     | 2
 b | 3     | 3
 a | 4     | 4

但我会很高兴得到这个(因为我可以很容易地用 过滤它distinct):

 k | min_v | max_v
---+-------+-------
 a | 1     | 2
 a | 1     | 2
 b | 3     | 3
 a | 4     | 4

是否可以使用 PostgreSQL 9.1+ 窗口函数来实现这一点?我试图了解是否可以让它k=a在此示例中的第一次和最后一次出现使用单独的分区(按 排序v)。

4

3 回答 3

11

这将使用示例数据返回您想要的结果。不确定它是否适用于现实世界的数据:

select k, 
       min(v) over (partition by group_nr) as min_v,
       max(v) over (partition by group_nr) as max_v
from (
    select *,
           sum(group_flag) over (order by v,k) as group_nr
    from (
    select *,
           case
              when lag(k) over (order by v) = k then null
              else 1
            end as group_flag
    from window_test
    ) t1
) t2
order by min_v;

我忽略了DISTINCT

于 2012-05-16T18:39:56.830 回答
1

编辑:我想出了以下查询——根本没有窗口函数:

WITH RECURSIVE tree AS (
  SELECT k, v, ''::text as next_k, 0 as next_v, 0 AS level FROM window_test
  UNION ALL
  SELECT c.k, c.v, t.k, t.v + level, t.level + 1
    FROM tree t JOIN window_test c ON c.k = t.k AND c.v + 1 = t.v),
partitions AS (
  SELECT t.k, t.v, t.next_k,
         coalesce(nullif(t.next_v, 0), t.v) AS next_v, t.level
    FROM tree t
   WHERE NOT EXISTS (SELECT 1 FROM tree WHERE next_k = t.k AND next_v = t.v))
SELECT min(k) AS k, v AS min_v, max(next_v) AS max_v
  FROM partitions p
 GROUP BY v
 ORDER BY 2;

我现在提供了 2 个工作查询,我希望其中一个适合您。

此变体的SQL Fiddle


实现此目的的另一种方法是使用支持序列。

  1. 创建支持序列:

    CREATE SEQUENCE wt_rank START WITH 1;
    
  2. 查询:

    WITH source AS (
      SELECT k, v,
             coalesce(lag(k) OVER (ORDER BY v), k) AS prev_k
        FROM window_test
        CROSS JOIN (SELECT setval('wt_rank', 1)) AS ri),
    ranking AS (
      SELECT k, v, prev_k,
             CASE WHEN k = prev_k THEN currval('wt_rank')
                  ELSE nextval('wt_rank') END AS rank
        FROM source)
    SELECT r.k, min(s.v) AS min_v, max(s.v) AS max_v
        FROM ranking r
        JOIN source s ON r.v = s.v
       GROUP BY r.rank, r.k
       ORDER BY 2;
    
于 2012-05-16T09:01:38.930 回答
0

如果不需要窗户、隔板或聚结,这对您来说不合适吗?它只是使用传统的 SQL 技巧来通过自连接查找最近的元组,并稍加区别:

SELECT k, min(v), max(v) FROM (
    SELECT k, v, v + min(d) lim FROM (
        SELECT x.*, y.k n, y.v - x.v d FROM window_test x
        LEFT JOIN window_test y ON x.k <> y.k AND y.v - x.v > 0) 
    z GROUP BY k, v, n)
w GROUP BY k, lim ORDER BY 2;

我认为这可能是一个更“相关”的解决方案,但我不确定它的效率。

于 2016-10-07T12:39:53.517 回答