Domanda

I have a table (or view) in my PostgreSQL database and want to do the following: Query the table and feed a function in my application subsequent n-tuples of rows from the query, but only those that satisfy some condition. I can do the n-tuple listing using a cursor, but I don't know how to do the condition checking on database level.

For example, the query returns:

3
2
4
2
0
1
4
6
2

And I want triples of even numbers. Here, they would be:

(2,4,2) (4,2,0) (4,6,2)

Obviously, I cannot discard the odd numbers from the query result. Instead using cursor, a query returning arrays in similar manner would also be acceptable solution, but I don't have any good idea how to use them to do this.

Of course, I could check it at application level, but I think it'd be cleaner to do it on database level. Is it possible?

È stato utile?

Soluzione

With the window function lead() (as mentioned by @wildplasser):

SELECT *
FROM  (
  SELECT tbl_id, i AS i1
       , lead(i)    OVER (ORDER BY tbl_id) AS i2
       , lead(i, 2) OVER (ORDER BY tbl_id) AS i3
  FROM   tbl
  ) sub
WHERE  i1%2 = 0
AND    i2%2 = 0
AND    i3%2 = 0;

There is no natural order of rows - assuming you want to order by tbl_id in the example.
% .. modulo operator

SQL Fiddle.

Altri suggerimenti

You can also use an array aggregate for this instead of using lag:

SELECT
  a[1] a1, a[2] a2, a[3] a3
FROM (
  SELECT
    array_agg(i) OVER (ORDER BY tbl_id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
  FROM
    tbl
) x(a)
WHERE a[1] % 2 = 0 AND a[2] % 2 = 0 AND a[3] % 2 = 0;

No idea if this'll be better, worse, or the same as Erwin's answer, just putting it in for completeness.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top