Re: Trying to find miss and mister of the last month with highest rating

From: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Trying to find miss and mister of the last month with highest rating
Date: 2011-07-07 16:25:34
Message-ID: CAADeyWgRbP7gzSVLyw5t+kbM-EzW_Tjv04h19_m0W4r2ryawSw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

This seems to work, but I wonder
if my query for "the miss of the last month"
could be improved

# select r.id, count(r.id), u.first_name, u.avatar, u.city
from pref_rep r, pref_users u
where r.nice=true and
to_char(current_timestamp - interval '1 month', 'IYYY-MM') =
to_char(r.last_rated, 'IYYY-MM') and
u.female=true and
r.id=u.id
group by r.id , u.first_name, u.avatar, u.city
order by count desc
limit 1;
id | count | first_name | avatar
| city
----------------+-------+------------+-----------------------------------------------------------
-------------+-----------
OK348033534186 | 49 | Елена |
http://i398.odnoklassniki.ru/getImage?photoId=194373317258
&photoType=0 | Хабаровск
(1 row)

(I'm sorry, I'm probably asking same
questions again and again and
not even not noticing it.
SQL is a tough language for me)

Should I maybe better use
date_trunc( 'month', now() ) - '1 MONTH'::INTERVAL
instead of comparing to_char() results?

Thank you
Alex

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Sullivan 2011-07-07 16:29:45 Re: Oracle to Postgres migration open source tool
Previous Message Tom Lane 2011-07-07 16:23:37 Re: Streaming replication on 9.1-beta2 after pg_restore is very slow