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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:37:18+00:00 2026-06-02T18:37:18+00:00

I have a query which needs to return the lowest balance for this month

  • 0

I have a query which needs to return the lowest balance for this month for each ID. The problem I am getting is it is returning multiple balances instead of the min balance on Balance.
I keep getting results like these:

ID      Name   Month     Year  TodayMonth   TodayYear  BalMin 
1        A      4         12     4            2012      10,000.00 
1        A      4         12     4            2012      20,000.00

When I need it to return just the lowest Balance:

ID      Name   Month     Year  TodayMonth   TodayYear  BalMin 
1        A      4         12     4            2012      10,000.00

Here is what I have so far:

SELECT DISTINCT
     TOP (100) PERCENT History.ID, info.Name, DATEPART(mm, History.ReportDate) AS Month, DATEPART(yy,History.ReportDate) AS Year, DATEPART(mm, { fn CURDATE() }) AS TodayMonth, DATEPART(yy, { fn CURDATE() }) AS TodayYear, MIN(History.Balance) AS BalMin
    FROM         History LEFT OUTER JOIN Info ON History.ID = Info.ID
    WHERE     (DATEPART(yy, History.ReportDate) = DATEPART(yy, { fn CURDATE() })) AND (DATEPART(mm, History.ReportDate) = DATEPART(mm, 
                          { fn CURDATE() })) AND (History.Balance > 0)
    GROUP BY History.ID, History.ReportDate, Info.Name, History.Balance
    ORDER BY History.ID
  • 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-02T18:37:21+00:00Added an answer on June 2, 2026 at 6:37 pm

    Your group by should not include balance (since you are running the min on it), and you should probably use the datepart that you are using in the select (otherwise you’re not grouping by month). The left join doesn’t make sense either since you are looking for non-null records in your where clause anyway. This might work:

    SELECT History.ID,
        Info.Name,
        DATEPART(mm, History.ReportDate) AS [Month],
        DATEPART(yy, History.ReportDate) AS [Year],
        MIN(History.Balance) AS BalMin
    FROM History
        JOIN Info ON History.ID = Info.ID
    WHERE (DATEPART(yy, History.ReportDate) = DATEPART(yy, { fn CURDATE() }))
        AND (DATEPART(mm, History.ReportDate) = DATEPART(mm, { fn CURDATE() }))
        AND (History.Balance > 0)
    GROUP BY History.ID,
        DATEPART(mm, History.ReportDate),
        DATEPART(yy, History.ReportDate),
        Info.Name
    ORDER BY History.ID
    

    I’ve also removed the distinct and top keywords, as you can see. Distinct is often unnecessary with group by (if not a code smell) and I’m assuming your top was for debugging or ordering a view, which you should probably not do.

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

Sidebar

Related Questions

So I have this app which needs to query entities from the Azure Tables
I have a query which produces a list of orders like this: Invoice Description
I have this query which works correctly in MySQL. More background on it here
I have an asp.net page which needs to return an object with a one-to-many
I have a query that needs to return results that are NOT matched in
I have a situation where I need to retrieve data from a query which
I need a query which can do the following operation. I have a table
I have a rather large table which I need to query for a reporting
I have a query which searches two separate fields in the same table... looking
I have a query which returns a series of cells of data from a

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.