Re: Problem with sql

From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: x asasaxax <xanaruto(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Problem with sql
Date: 2008-10-29 05:46:36
Message-ID: 20081029054636.GC596@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Oct 28, 2008 at 11:36:12PM -0300, x asasaxax wrote:
> table_1: product_code, atualization_date
> table_2: id, date, value
> I need to get the value of table 2 that has the greatest date that is minor
> that atualization_date.
> The select should return something like that:
> unique product_code´s, with their atualization_date and their value

select t1.*,
(select t2.value from table_2 as t2 where t2.date < t1.atualization_date order by t2.date desc limit 1)
from table_1 as t1
;

depesz

--
Linkedin: http://www.linkedin.com/in/depesz / blog: http://www.depesz.com/
jid/gtalk: depesz(at)depesz(dot)com / aim:depeszhdl / skype:depesz_hdl / gg:6749007

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joshua D. Drake 2008-10-29 06:13:20 Re: Decreasing WAL size effects
Previous Message David Wilson 2008-10-29 03:43:58 Re: postgresql and Mac OS X