Re: Please help me write a query

From: Justin Graf <justin(at)magwerks(dot)com>
To: Nikolas Everett <nik9000(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Please help me write a query
Date: 2010-05-27 16:16:56
Message-ID: 4BFE9AF8.7000206@magwerks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 5/27/2010 9:04 AM, Nikolas Everett wrote:
> Say I have a table that stores state transitions over time like so:
> id, transitionable_id, state1, state2, timestamp
>
> I'm trying to write a query that coalesces changes in state2 away to
> produce just a list of transitions of state1. I guess it would look
> something like
>
> SELECT state1, FIRST(timestamp)
> FROM table
>
> but I have no idea how to aggregate just the repeated state1 rows.

if i understand what your after

Select distinct transitinable_id, state1, min(timestamp) from table
group by transitinable_id, state1

All legitimate Magwerks Corporation quotations are sent in a .PDF file attachment with a unique ID number generated by our proprietary quotation system. Quotations received via any other form of communication will not be honored.

CONFIDENTIALITY NOTICE: This e-mail, including attachments, may contain legally privileged, confidential or other information proprietary to Magwerks Corporation and is intended solely for the use of the individual to whom it addresses. If the reader of this e-mail is not the intended recipient or authorized agent, the reader is hereby notified that any unauthorized viewing, dissemination, distribution or copying of this e-mail is strictly prohibited. If you have received this e-mail in error, please notify the sender by replying to this message and destroy all occurrences of this e-mail immediately.
Thank you.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Justin Graf 2010-05-27 16:36:20 Re: Please help me write a query
Previous Message Rainer Pruy 2010-05-27 16:12:39 UPDATE...RETURNING col INTO var - what is returned?