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 9114693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:15:25+00:00 2026-06-17T04:15:25+00:00

I have a postgres table with customer ID’s, dates, and integers. I need to

  • 0

I have a postgres table with customer ID’s, dates, and integers. I need to find the average of the top 3 records for each customer ID that have dates within the last year. I can do it with a single ID using the SQL below (id is the customer ID, weekending is the date, and maxattached is the integer).

One caveat: the maximum values are per month, meaning we’re only looking at the highest value in a given month to create our dataset, thus why we’re extracting month from the date.

SELECT 
  id,
  round(avg(max),0) 
FROM 
  (
   select 
     id,
     extract(month from weekending) as month,
     extract(year from weekending) as year,
     max(maxattached) as max 
   FROM 
     myTable 
   WHERE
     weekending >= now() - interval '1 year' AND 
     id=110070 group by id,month,year 
   ORDER BY
     max desc limit 3
   ) AS t 
GROUP BY id;

How can I expand this query to include all ID’s and a single averaged number for each one?

Here is some sample data:

ID     | MaxAttached | Weekending
110070 | 5           | 2011-11-10
110070 | 6           | 2011-11-17
110071 | 4           | 2011-11-10
110071 | 7           | 2011-11-17
110070 | 3           | 2011-12-01
110071 | 8           | 2011-12-01
110070 | 5           | 2012-01-01
110071 | 9           | 2012-01-01

So, for this sample table, I would expect to receive the following results:

ID     | MaxAttached

110070 | 5           
110071 | 8

This averages the highest value in a given month for each ID (6,3,5 for 110070 and 7,8,9 for 110071)

Note: postgres version 8.1.15

  • 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-06-17T04:15:26+00:00Added an answer on June 17, 2026 at 4:15 am

    First – get the max(maxattached) for every customer and month:

    SELECT id,
           max(maxattached) as max_att         
    FROM myTable 
    WHERE weekending >= now() - interval '1 year' 
    GROUP BY id, date_trunc('month',weekending);
    

    Next – for every customer rank all his values:

    SELECT id,
           max_att,
           row_number() OVER (PARTITION BY id ORDER BY max_att DESC) as max_att_rank
    FROM <previous select here>;
    

    Next – get the top 3 for every customer:

    SELECT id,
           max_att
    FROM <previous select here>
    WHERE max_att_rank <= 3;
    

    Next – get the avg of the values for every customer:

    SELECT id,
           avg(max_att) as avg_att
    FROM <previous select here>
    GROUP BY id;
    

    Next – just put all the queries together and rewrite/simplify them for your case.

    UPDATE: Here is an SQLFiddle with your test data and the queries: SQLFiddle.

    UPDATE2: Here is the query, that will work on 8.1 :

    SELECT customer_id,
           (SELECT round(avg(max_att),0)
            FROM (SELECT max(maxattached) as max_att         
                  FROM table1
                  WHERE weekending >= now() - interval '2 year' 
                    AND id = ct.customer_id
                  GROUP BY date_trunc('month',weekending)
                  ORDER BY max_att DESC
                  LIMIT 3) sub 
            ) as avg_att
    FROM customer_table ct;
    

    The idea – to take your initial query and run it for every customer (customer_table – table with all unique id for customers).

    Here is SQLFiddle with this query: SQLFiddle.

    Only tested on version 8.3 (8.1 is too old to be on SQLFiddle).

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

Sidebar

Related Questions

is is possible in postgres to have a trigger on CREATE TABLE that will
I have simple table creating script in Postgres 9.1. I need it to create
I have a postgres table that looks in part like: Year | Month |
I have a postgres database with a table that contains rows I want to
I have a postgres table that has the following fields start_date,duration duration contains any
I have a postgres table that contains a very large number of numerals of
I'm dealing with a Postgres table (called lives) that contains records with columns for
I have a (Postgres) DB table that I'd like to add a manual 'sort'
I'm working on rails application with postgres db.I have a table called merchant_review_votes where
Postgres 9.0.4 Rails 3.0.7 AR 3.0.7 pg 0.12.2 I have a table with 3

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.