Re: how to remove a for-loop from programming language and put it into the query?

From: Pedro Zorzenon Neto <pedro2009(at)mandic(dot)com(dot)br>
To: Thom Brown <thombrown(at)gmail(dot)com>
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:26:43
Message-ID: 4C31F9B3.8050607@mandic.com.br
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> 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!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next 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?
Previous Message Sam Mason 2010-07-05 15:22:05 Re: how to remove a for-loop from programming language and put it into the query?