pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jim Buttafuoco <jim@contactbda.com>
To: FC <lne-1mc8@myamail.com>
To: pgsql-general@postgresql.org
Subject: Re: SQL question.
Date: Wed, 1 Jun 2005 11:20:08 -0400
Message-ID: <20050601151852.M49412@contactbda.com> (raw)
In-Reply-To: <6EE9301F-4C50-4160-9DC0-667826764A74@myamail.com>
References: <6EE9301F-4C50-4160-9DC0-667826764A74@myamail.com>


how about (untested)

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
) A
ORDER BY read_date DESC limit 100


---------- Original Message -----------
From: FC <lne-1mc8@myamail.com>
To: pgsql-general@postgresql.org
Sent: Wed, 1 Jun 2005 16:40:48 +0200
Subject: [GENERAL] SQL question.

> Hello SQL Aces !
> 
> I want to do a select on a table distinct on linkid and sorted by  
> date. I have try this
> 
> 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;
> 
> With this request "works" but the raws are sorted as I want. To have  
> the rows sorted by date I have done this using a temporary table and  
> that works exactly as I want.
> 
> CREATE LOCAL TEMP TABLE temp_links WITHOUT OIDS AS
>    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;
> 
> SELECT * from temp_links ORDER BY read_date DESC limit 100
> 
> 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.
> 
> Thanks for any suggestions.
> Fred
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
------- End of Original Message -------




view thread (71+ messages)  latest in thread

Message-ID: <20050601151852.M49412@contactbda.com>
Permalink:  ../20050601151852.M49412@contactbda.com/
Also on:    postgresql.org/message-id/20050601151852.M49412@contactbda.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: jim@contactbda.com, lne-1mc8@myamail.com
  Subject: Re: SQL question.
  In-Reply-To: <20050601151852.M49412@contactbda.com>
  References: <6EE9301F-4C50-4160-9DC0-667826764A74@myamail.com> <20050601151852.M49412@contactbda.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox