Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6142067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:20:14+00:00 2026-05-23T18:20:14+00:00

At my Drupal website users can rate each other and those timestamped ratings are

  • 0

At my Drupal website users can rate each other and those timestamped ratings are stored in the pref_rep table:

# select id, nice, last_rated from pref_rep where nice=true
  order by last_rated desc limit 7;
           id           | nice |         last_rated
------------------------+------+----------------------------
 OK152565298368         | t    | 2011-07-07 14:26:38.325716
 OK452217781481         | t    | 2011-07-07 14:26:10.831353
 OK524802920494         | t    | 2011-07-07 14:25:28.961652
 OK348972427664         | t    | 2011-07-07 14:25:17.214928
 DE11873                | t    | 2011-07-07 14:25:05.303104
 OK335285460379         | t    | 2011-07-07 14:24:39.062652
 OK353639875983         | t    | 2011-07-07 14:23:33.811986

Also I keep the gender of each user in the pref_users table:

# select id, female from pref_users limit 7;
       id       | female
----------------+--------
 OK351636836012 | f
 OK366097485338 | f
 OK251293359874 | t
 OK7848446207   | f
 OK335478250992 | t
 OK355400714550 | f
 OK146955222542 | t

I’m trying to create 2 Drupal blocks displaying “Miss last month” and “Mister last month”, but my question is not about Drupal, so please don’t move it to drupal.stackexchange.com 😉

My question is about SQL: how could I find the user with the highest count of nice – and that for the last month? I would have 2 queries – one for female and one for non-female.

Using PostgreSQL 8.4.8 / CentOS 5.6 and SQL is sometimes so hard 🙂

Thank you!
Alex

UPDATE:

I’ve got a nice suggestion to cast timestamps to strings in order to find records for the last month (not for the last 30 days)

UPDATE2:

I’ve ended up doing string comparison:

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(r.id) desc
limit 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:20:15+00:00Added an answer on May 23, 2026 at 6:20 pm

    Say you run it once on the first day of the month, and cache the results, since counting votes on every page is kinda useless.

    First some date arithmetic :

    SELECT now(), 
           date_trunc( 'month', now() ) - '1 MONTH'::INTERVAL, 
           date_trunc( 'month', now() );
    
                  now              |        ?column?        |       date_trunc       
    -------------------------------+------------------------+------------------------
     2011-07-07 16:24:38.765559+02 | 2011-06-01 00:00:00+02 | 2011-07-01 00:00:00+02
    

    OK, we got the bounds for the “last month” datetime range.
    Now we need some window function to get the first rows per gender :

    SELECT * FROM (
       SELECT *, rank( ) over (partition by gender order by score desc ) 
       FROM (
          SELECT user_id, count(*) AS score FROM pref_rep 
          WHERE nice=true 
          AND last_rated >= date_trunc( 'month', now() ) - '1 MONTH'::INTERVAL
          AND last_rated <  date_trunc( 'month', now() )
          GROUP BY user_id) s1 
       JOIN users USING (user_id)) s2 
    WHERE rank=1;
    

    Note this can give you several rows in case of ex-aequo.

    EDIT :

    I’ve got a nice suggestion to cast timestamps to strings in order to
    find records for the last month (not for the last 30 days)

    date_trunc() works much better.

    If you make 2 queries, you’ll have to make the count() twice. Since users can potentially vote many times for other users, that table will probably be the larger one, so scanning it once is a good thing.

    You can’t “leave joining back onto the users table to the outer part of the query too” because you need genders…

    Query above takes about 30 ms with 1k users and 100k votes so you’d definitely want to cache it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you help me to understand, how do I do Drupal website deployment and
I'm building a website using Drupal. On the header of each page I want
when I login as user to my drupal website I can see the tabs
I've a Drupal A website with users accounts running on server A and with
When new users sign up on my Drupal website, I would like to send
I have a Drupal website embedding a Flash game. The registered website users are
I want to create a registration form for dealers in my drupal website.Can any
on my website i have two types of files that end users can download
I'm upgrading a drupal website. There are thousands of users and it is using
as the single user / developer on a drupal website im experience serious performance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.