From: | Lew <noone(at)lewscanon(dot)com> |
---|---|
To: | pgsql-novice(at)postgresql(dot)org |
Subject: | Re: Select only active entries |
Date: | 2010-07-27 12:32:38 |
Message-ID: | i2mjkn$t5i$1@news.albasani.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
Carel Combrink wrote:
> I want to create a query to select only the latest ones that are active,
...
>> My table is defined as follow:
>>
>> =# CREATE TABLE test_select
>> (
>> id integer,
>> is_active boolean,
>> idx serial NOT NULL,
>> CONSTRAINT test_select_pkey PRIMARY KEY (idx)
>> );
...
>> How would the query look based on the 'idx' column?
>> IF I replace 'ctid' with 'idx' I get:
>> =# select id, is_active from (select id, is_active, max(idx), row_number()
>> over (partition by id) from test_select group by id, is_active, idx order by
>> id, idx desc) foo where is_active and row_number = 1;
>> id | is_active
>> ----+-----------
>> 5 | t
>> 6 | t
>> 7 | t
>> 9 | t
>> 10 | t
>> (5 rows)
>>
>> Or if I must use a timestamp column how would I structure the query?
Michael Wood wrote:
> No, you don't need a timestamp if you have your idx column. It serves
> the same purpose.
Until it wraps around.
--
Lew
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2010-07-27 19:59:11 | Re: could not change directory to "/root" |
Previous Message | Teri Holmes | 2010-07-26 23:40:19 | Corrupt Database recovery |