[GENERAL] SQL question.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[GENERAL] SQL question.

fc-6

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 [hidden email])
Reply | Threaded
Open this post in threaded view
|

Re: [GENERAL] SQL question.

Jim Buttafuoco

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 <[hidden email]>
To: [hidden email]
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 [hidden email])
------- End of Original Message -------


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
      subscribe-nomail command to [hidden email] so that your
      message can get through to the mailing list cleanly
Reply | Threaded
Open this post in threaded view
|

Re: [GENERAL] SQL question.

Martijn van Oosterhout
In reply to this post by fc-6
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   <[hidden email]>   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.

attachment0 (240 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [GENERAL] SQL question.

fc-6
In reply to this post by Jim Buttafuoco

I need to remember to keep things simple...

Works fine, thanks.

Fred


On Jun 1, 2005, at 5:20 PM, Jim Buttafuoco wrote:

> 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 <[hidden email]>
> To: [hidden email]
> 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  
>> [hidden email])
>>
> ------- End of Original Message -------
>
>
>


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
      subscribe-nomail command to [hidden email] so that your
      message can get through to the mailing list cleanly
Reply | Threaded
Open this post in threaded view
|

Character conversion...

fc-6
In reply to this post by Jim Buttafuoco

I have all my database encoded in ISO-8859-1 and I would like to  
convert this database in UNICODE.

I have try this but with no success.

pg_dump dbase > foo.db
createdb -E UNICODE dbase
psql dbase < foo.db

I have also try to modify the value of client_encoding in the foo.db  
file but that didn't fix my problem.

Thanks.
Fred


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to [hidden email]