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

  • Home
  • SEARCH
  • 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 9183547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:49:53+00:00 2026-06-17T18:49:53+00:00

I have a database table that looks like this called Totals and I’m trying

  • 0

I have a database table that looks like this called Totals and I’m trying to select the max date per month per per person so I can average the person’s balance over the months

 Date       Person     Balance
01-15-12       A        79
01-23-12       c        150
01-17-12       A        65
02-23-12       c        150
02-15-12       A        70
03-23-12       c        15
03-15-12       A        705
03-28-12       c        150
04-15-12       A        700
04-23-12       c        150

I’m joining this table to a temp table called #bal which contains just people like A B C …etc
So for each month I just want the max row per month per person so that I can sum the balances and find the average balance over the months per person.

  create table #bal 
 (  
 person bigint,
 avgbal decimal,
 mxdate datetime

 )
  insert into #bal(person,avgbal,mxdate)
  select 
  b.person,
  (sum(s.BAL)/12) as avgbal,
  max(date) as mxdate 

  from #bal b
  inner join TOTALS s on (b.person=s.person)
  where DATE between '01-17-2012' and getdate()
  group by b.person

Have something like this so far that’s grouping by date but I just want to select the max day per month.

  • 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-17T18:49:54+00:00Added an answer on June 17, 2026 at 6:49 pm

    I’ve produced a couple of samples based on some sample data I created based on the set above. I’m not sure whether you want the last value in each month or the max value, as these aren’t necessarily the same, so I’ve written basic queries for both:

    declare @table table
    (
    date date,
    person varchar(10),
    balance int
    )
    
    insert into @table
    select '01-15-12', 'A', 79
    union all
    select '01-23-12', 'c', 150
    union all
    select '01-17-12', 'A', 65
    union all
    select '02-23-12', 'c', 150
    union all
    select '02-15-12', 'A', 70
    union all
    select '03-23-12', 'c', 15
    union all
    select '03-15-12', 'A', 705
    union all
    select '03-28-12', 'c', 150
    union all
    select '04-15-12', 'A', 700
    union all
    select '04-23-12', 'c', 150;
    
    -- Average of max balance in month
    with maxMonth as
    (
      select year = year(date)
        , month = month(date)
        , person, monthMaxBalanace = max(balance)
      from @table
      where date between '01-17-2012' and getdate()
      group by year(date), month(date), person
    )
    select person, maxInMonthAverage = avg(monthMaxBalanace)
    from maxMonth
    group by person;
    

    Or if you need to use the last balance in each month you can change the query:

    -- Average of last balance in month
    with lastInMonth as
    (
      select year = year(date)
        , month = month(date)
        , person, balance
        , monthRank = row_number() over (partition by year(date), month(date), person order by date desc)
      from @table
      where date between '01-17-2012' and getdate()
    ),
    lastRows as
    (
      select * from lastInMonth where monthRank = 1
    )
    select person, lastInMonthAverage = avg(balance)
    from lastRows
    group by person;
    

    Based on your example query (i.e. 17-Jan and greater) the results are the same, but if you include the value from the 15th they are slightly different due to the different logic in the two queries.

    enter image description here

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

Sidebar

Related Questions

I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have one table that looks like this called survey_1: ================================================ |id | token
Let's say I have a table called my_table that looks like this: id |
I have some code to update a database table that looks like try {
I have a self-referential table in my database that looks sort of like above.
I have a database table called users This table has two columns (that are
that my problem: I have database table like that: id (AI) market_id 1 6
So in javascript I have an array that looks about like this: [{x1:0,x2:2000,y:300},{x1:50,x2:250,y:500}] And
I have a table called Subscriptions. I'd like to redirect any LINQ select from
I have a table in my MySQL database called 'children'. In that table is

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.