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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:47:38+00:00 2026-05-31T23:47:38+00:00

I am trying to understand how to do a Sum when I need to

  • 0

I am trying to understand how to do a Sum when I need to only use the Max/top values for a date period. This is kind of like SQL Server: SELECT only the rows with MAX(DATE) but not exactly. Thanks for reading.

declare @tbl Table ( tableid int , eventdate date,  valuec char(5) , valued int , recordedwhen datetime2(3) )
insert into @tbl values ( 0 , '2012-03-22' , '11111' , 3 , '2012-03-23 17:21:01.083' )
insert into @tbl values ( 1 , '2012-03-22' , '22222' , 3 , '2012-03-23 17:21:01.083' )
insert into @tbl values ( 2 , '2012-03-22' , '22222' , 4 , '2012-03-23 18:21:01.083' )
insert into @tbl values ( 3 , '2012-03-22' , '22222' , 5 , '2012-03-23 18:21:01.083' )

select 
        eventdate, valuec , sum(valued) as valuedSum , recordedwhen
        from 
        @tbl
        group by eventdate, valuec, recordedwhen

I get three rows. But what I actually want is this:

--   
--   eventdate  valuec valuedSum   recordedwhen
--   ---------- ------ ----------- ----------------------
--   2012-03-22 11111  3           2012-03-23 17:21:01.08
--   2012-03-22 22222  9           2012-03-23 18:21:01.08

I do not want the row where tableid=1 because that is part of an older batch of data.
The newer rows represent a later insert (all recordedwhen values with the same datetime are the same for a batch)

So if a new row was inserted like this:

insert into @tbl values ( 4 , '2012-03-22' , '22222' , 6 , '2012-03-24 18:21:01.083' )

Then the data would should like this:

--   eventdate  valuec valuedSum   recordedwhen
--   ---------- ------ ----------- ----------------------
--   2012-03-22 11111  3           2012-03-23 17:21:01.08
--   2012-03-22 22222  6           2012-03-24 18:21:01.08
  • 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-31T23:47:38+00:00Added an answer on May 31, 2026 at 11:47 pm

    You basically need two selects. You can do either an inner select, or CTE. The following produces the result you desire

    create table #tbl ( tableid int , eventdate date,  valuec char(5) , valued int , recordedwhen datetime2(3) )
    insert into #tbl values ( 0 , '2012-03-22' , '11111' , 3 , '2012-03-23 17:21:01.083' )
    insert into #tbl values ( 1 , '2012-03-22' , '22222' , 3 , '2012-03-23 17:21:01.083' )
    insert into #tbl values ( 2 , '2012-03-22' , '22222' , 4 , '2012-03-23 18:21:01.083' )
    insert into #tbl values ( 3 , '2012-03-22' , '22222' , 5 , '2012-03-23 18:21:01.083' )
    insert into #tbl values ( 4 , '2012-03-22' , '22222' , 6 , '2012-03-24 18:21:01.083' )
    
    ;
    with t (eventdate, valuec, maxrecordedwhen)
    as (select eventdate, valuec, max(recordedwhen)
        from #tbl
        group by eventdate, valuec)
    select t.eventdate, t.valuec, sum(valued) as valuedsum, maxrecordedwhen
    from t
        join #tbl on t.eventdate = #tbl.eventdate and t.valuec = #tbl.valuec
    where t.maxrecordedwhen = #tbl.recordedwhen
    group by t.eventdate, t.valuec, t.maxrecordedwhen
    
    drop table #tbl
    

    Notice that the maximum batch date is found in the CTE and then used to filter the summation result.

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

Sidebar

Related Questions

Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand why this doesn't work. I keep getting the following errors: left
I'm trying to use the SUM function to count rows from 3 tables, which
I've been trying to use Firebug's profiler to better understand the source of some
I'm trying to use group by with rollup clause within sql server 2005 but
Trying to understand this open source app on github, it has a gem file:
So basically I'm trying to replicate this query as a stored procedure. Use ThisDB
I am trying to understand the meaning and use of the param parameter in
Trying to understand the use of verifySet etc... but unless I do a workaround
Trying to understand how you're supposed to read files in python. This is what

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.