Re: Trying to find miss and mister of the last month with highest rating - Mailing list pgsql-general

From Alexander Farber
Subject Re: Trying to find miss and mister of the last month with highest rating
Date
Msg-id CAADeyWgRbP7gzSVLyw5t+kbM-EzW_Tjv04h19_m0W4r2ryawSw@mail.gmail.com
Whole thread Raw
In response to Re: Trying to find miss and mister of the last month with highest rating  (Alexander Farber <alexander.farber@gmail.com>)
List 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

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Streaming replication on 9.1-beta2 after pg_restore is very slow
Next
From: Andrew Sullivan
Date:
Subject: Re: Oracle to Postgres migration open source tool