Re: Complex query

From: Leonardo M(dot) Ramé <l(dot)rame(at)griensu(dot)com>
To: David Johnston <polobo(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Complex query
Date: 2014-03-31 19:22:42
Message-ID: 20140331192242.GD10833@leonardo-laptop
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2014-03-31 12:16:53 -0700, David Johnston wrote:
> Leonardo M. Ramé-2 wrote
> > select lag.id, lag.idtask, lag.code, lag.lg from (select idtask, code, id,
> > lag(code, -1) over () as lg from tasks_test) as lag
>
> First you want to include an ORDER BY in the OVER(...) clause, and probably
> a PARTITION BY as well.
>
> Then you move that to a sub-query (for example):
>
> SELECT *
> FROM tbl
> WHERE tbl.idtask IN (
> SELECT lag.idtask FROM ( lag_query_here ) lag WHERE lag.code = 'T' and
> lag.lg = 'S'
> );
>
> David J.
>

Great!, that's what I needed, thank you.

--
Leonardo M. Ramé
Medical IT - Griensu S.A.
Av. Colón 636 - Piso 8 Of. A
X5000EPT -- Córdoba
Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19
Cel.: +54 9 (011) 40871877

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ben Hoyt 2014-03-31 19:28:14 Getting sequence-generated IDs from multiple row insert
Previous Message Adrian Klaver 2014-03-31 19:21:03 Re: [ADMIN]openvz and shared memory trouble