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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:57:41+00:00 2026-05-25T21:57:41+00:00

Suppose I have a table: ACCOUNT It has columns: ID , NAME , BALANCE

  • 0

Suppose I have a table: ACCOUNT
It has columns: ID, NAME, BALANCE

I want to determine if the SUM of balances of people who has balance more than $ 2,000 is greater than $ 50,000. It means I want to select a logical value. Because I don’t want to get specific sum, I only need comparison result. I want to be able to write something like:

SELECT SUM(BALANCE) > 50000 
  FROM ACCOUNT
 WHERE BALANCE > 2000

Sql Server says it is syntax error. So I have to do:

SELECT SUM(BALANCE)  
  FROM ACCOUNT
 WHERE BALANCE > 2000

Then compare the result to 50000. But taking sum of all the items after reaching 50000 is redundant since I only compare it to 50000.

So how can I write this query in a way to stop after reaching comparison value and give me the logical result?

  • 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-25T21:57:42+00:00Added an answer on May 25, 2026 at 9:57 pm

    This question was asked in 2011. In versions prior to 2012 there isn’t really a clean way of doing this and getting SQL Server to short circuit the scan as soon as the total is reached.

    You could use a recursive CTE to add up the balances instead. The exit condition for this could be once the balance running total exceeds 50,000. Dependant on table cardinality and indexes this could be either more or less efficient.

    You could also have a custom CLR aggregate that terminated processing by throwing an error if 50,000 was reached but quite a horrible solution!

    For versions since 2012 see my answer here

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

Sidebar

Related Questions

Suppose I have a table with columns Name VARCHAR, Department VARCHAR, Gender VARCHAR, and
Suppose I have table Person table Employee, which inherits Person. I want to get
Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose I have a tags table with two columns: tagid and contentid . Each
Suppose I have a table with columns (DayId, RunningTotal): DayId RunningTotal --------------------- 1 25
Suppose I have a table amountInfo with columns (id, amount1, amount2, amount3) where amountX
Suppose I have a table called 'test', into which there are three columns named
Suppose I have table first_table which has an FK to table second_table table second_table
suppose I have this table var 10 3 4 60 70 and I want
Suppose I have a table with columns userdef1, userdef2 .....userdef6 and I have another

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.