using min|max in where

From: Ben Carbery <ben(dot)carbery(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: using min|max in where
Date: 2010-08-25 00:41:21
Message-ID: AANLkTinbg96cBzS+w31o75=wYUmHZxmwUJh4bKQc1Az2@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi, I have some sql like so:

SELECT min(date) INTO d FROM interest_rate WHERE m_code = NEW.code;
UPDATE interest_rate SET date = NEW.start_date, rate = NEW.initial_rate
WHERE m_code = NEW.code AND date = d;

Actually this is pgsql but I don't think that matters.

I am wondering if I can make this more compact by somehow including the
'min' function in the WHERE clause, but WITHOUT simply moving the select in
there. So not this..

UPDATE interest_rate SET date = NEW.start_date, rate = NEW.initial_rate
WHERE m_code = NEW.code AND date = (SELECT min(date) FROM interest_rate
WHERE m_code = NEW.code);

This is just an example but I seem to find this pattern a lot in my
functions.

B

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Bruce Momjian 2010-08-25 14:46:11 Re: using min|max in where
Previous Message Jann Röder 2010-08-23 10:58:06 Re: Inefficient query plan