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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:12:49+00:00 2026-05-13T17:12:49+00:00

I have table where I need to select MAX(SUM(total)) of a periods total. SELECT

  • 0

I have table where I need to select MAX(SUM(total)) of a periods total.

SELECT SUM(P.amount) as total FROM bank P WHERE P.ReceivedDate >= '2008-07-28' AND P.ReceivedDate <= '2008-08-31';

SELECT SUM(P.amount) as total FROM bank P WHERE P.ReceivedDate >= '2008-09-01' AND P.ReceivedDate <= '2008-09-28';

SELECT SUM(P.amount) as total FROM bank P WHERE P.ReceivedDate >= '2008-09-29' AND P.ReceivedDate <= '2008-10-26';

I have run the above queries and find the Highest total in php.

I am not sure how to group by this query.

I am expecting a single query to replace all this query to MAX(SUM(total)).

please help me out.

  • 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-13T17:12:49+00:00Added an answer on May 13, 2026 at 5:12 pm

    You could try something like

    SELECT  TOP 1
            CASE
                WHEN P.ReceivedDate BETWEEN '2008-07-28' AND '2008-08-31' THEN 1
                WHEN P.ReceivedDate BETWEEN '2008-09-01' AND '2008-09-28' THEN 2
                WHEN P.ReceivedDate BETWEEN '2008-09-29' AND '2008-10-26' THEN 3
            END,
            SUM(P.amount) Total
    FROM    bank p
    GROUP BY CASE
                WHEN P.ReceivedDate BETWEEN '2008-07-28' AND '2008-08-31' THEN 1
                WHEN P.ReceivedDate BETWEEN '2008-09-01' AND '2008-09-28' THEN 2
                WHEN P.ReceivedDate BETWEEN '2008-09-29' AND '2008-10-26' THEN 3
            END
    
    HAVING  CASE 
                WHEN P.ReceivedDate BETWEEN '2008-07-28' AND '2008-08-31' THEN 1 
                WHEN P.ReceivedDate BETWEEN '2008-09-01' AND '2008-09-28' THEN 2 
                WHEN P.ReceivedDate BETWEEN '2008-09-29' AND '2008-10-26' THEN 3 
            END IS NOT NULL
    ORDER BY Total DESC
    

    or a shorter version

    SELECT  TOP 1
            SUM(amount) Total
    FROM    (
                SELECT  CASE 
                            WHEN P.ReceivedDate BETWEEN '2008-07-28' AND '2008-08-31' THEN 1 
                            WHEN P.ReceivedDate BETWEEN '2008-09-01' AND '2008-09-28' THEN 2 
                            WHEN P.ReceivedDate BETWEEN '2008-09-29' AND '2008-10-26' THEN 3 
                        END GroupVal,
                        P.amount
                FROM    bank P
            ) Vals
    GROUP BY Vals.GroupVal
    HAVING  Vals.GroupVal IS NOT NULL
    ORDER BY Total DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three table I need to query table 1,2 and 3 and select
I have a table with a datetime record. I need to select the record
need help/guide for sql select query, I have 2 table stock and stock_history, in
I have multiple tables that need to be merged into one. SELECT name, SUM(money)
So I need to select a value from my table and modify it slightly
I have a report that I need to create from a UserHistory table in
I have a table OrderSpecs . I need to select all of the rows
I have this query: DECLARE @t TABLE(NAME NVARCHAR(MAX),datee date,val money) insert INTO @t SELECT
I have two table and I need to copy the data across from SRCServiceUsers
I have table 'products' and I need to update 'price' field by 20% if

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.