From: | Thom Brown <thombrown(at)gmail(dot)com> |
---|---|
To: | Pedro Zorzenon Neto <pedro2009(at)mandic(dot)com(dot)br> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: how to remove a for-loop from programming language and put it into the query? |
Date: | 2010-07-05 15:32:55 |
Message-ID: | AANLkTilmun6jb80AMDHh97gZNmE6TJOkF0ClwSmqeyTu@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 5 July 2010 16:26, Pedro Zorzenon Neto <pedro2009(at)mandic(dot)com(dot)br> wrote:
>> I'm probably misunderstanding the problem, but can't you just do:
>>
>> SELECT
>> diag_value
>> FROM
>> diagnose_logs
>> WHERE
>> ts <= '2009-12-25 23:59:59'
>> AND
>> hardware_id BETWEEN 1 AND 500
>> ORDER BY
>> ts DESC
>> LIMIT 1
>
> Hi Thom,
>
> Yes, I think you misunderstood.
>
> An example of a table:
>
> hardware_id | ts | diag_value
> 1 | 2009-12-25 14:00:00 | 43.5 (*)
> 1 | 2009-12-26 15:00:00 | 43.6
> 1 | 2009-12-24 13:00:00 | 43.7
> 2 | 2009-12-24 15:00:00 | 43.8 (*)
> 2 | 2009-12-24 14:00:00 | 43.9
> 2 | 2009-12-24 14:16:00 | 43.9
> 2 | 2009-12-27 14:00:00 | 44.0
>
> I need to get the "most recent" value before "2009-12-25 23:59:59" from
> every hardware_id.
>
> For hardware_id=1, the value would be:
> 1 | 2009-12-25 14:00:00 | 43.5
> for hardware_id=2, the value would be:
> 2 | 2009-12-24 15:00:00 | 43.8
>
> I need a query that will return me those lines marked with (*) :-) is
> this possible?
>
> Thanks!
>
>
D'oh! I completely ignored that "LIMIT 1". okay... what Sam said.
Thom
From | Date | Subject | |
---|---|---|---|
Next Message | Pedro Zorzenon Neto | 2010-07-05 15:44:55 | Re: how to remove a for-loop from programming language and put it into the query? |
Previous Message | Tim Landscheidt | 2010-07-05 15:30:23 | Re: how to remove a for-loop from programming language and put it into the query? |