Re: SQL question. - Mailing list pgsql-general

From Martijn van Oosterhout
Subject Re: SQL question.
Date
Msg-id 20050601153255.GB29582@svana.org
Whole thread Raw
In response to SQL question.  (FC <lne-1mc8@myamail.com>)
List pgsql-general
On Wed, Jun 01, 2005 at 04:40:48PM +0200, FC wrote:
>
> Hello SQL Aces !
>
> I want to do a select on a table distinct on linkid and sorted by
> date. I have try this

How about a subquery?:

SELECT * FROM
 ( SELECT DISTINCT ON (linkid) * FROM all_links
   WHERE uid='2' AND DATE_TRUNC('day',read_date) = DATE_TRUNC('day',
         TIMESTAMP '2005-06-01') ORDER BY linkid, read_date) AS sub
ORDER BY read_date DESC limit 100;

Hope this helps,

> My question is... How can I do the same thing in the more efficient
> way and without using a temporary table. Since I am using PHP and the
> table is not deleted at the end of the program because PHP keeps the
> connection to the database open.
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

Attachment

pgsql-general by date:

Previous
From: "Gerald D. Anderson"
Date:
Subject: Old problem needs solution
Next
From: Matt Miller
Date:
Subject: Autonomous Transactions