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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:57:07+00:00 2026-05-17T19:57:07+00:00

Say I have a bunch of rows in a DB (SQLServer 2008 in this

  • 0

Say I have a bunch of rows in a DB (SQLServer 2008 in this case) that can be used to create equations.

 -----------------------------------------------------
 OperationID | EquationID | Operation | Amount | Order
 -----------------------------------------------------
     1       |     1      |     +     |   12   |  1 
     2       |     1      |     +     |   12   |  2 
     3       |     2      |     /     |   2    |  3 
     4       |     2      |     +     |   12   |  1 
     5       |     2      |     -     |   2    |  2 
 -----------------------------------------------------

I need come up with a way to evaluate the equations in this table.

Equation 1: 12 + 12 = 24
Equation 2: (12 – 2)/2 = 5

I cannot think of a way to get these results without iterating through the rows. The only ways I know how to do this is with a cursor or through the use of a temp table and a while loop. Are there any better ways to do this? If not generally what will perform better cursors or while loops?

Note: This is somewhat simplified and at this stage in the project we can only conjecture about what the data will look like. The assumption is that each ‘equation’ will have around 100 to 1000 operations and that there will be a few thousand ‘equations’ each day that will need to be processed.

  • 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-17T19:57:07+00:00Added an answer on May 17, 2026 at 7:57 pm

    It’s been demonstrated that a recursive CTE performs better than a loop for coming up with running totals. This is just a running total with a variable operator really, so the performance benefit should apply here.

    The way to create a recursive CTE which behaves like a loop is like so:

            ;WITH cte AS (
            SELECT equation, number, order FROM table WHERE order = 1
            UNION ALL
            SELECT table.equation, 
                CASE WHEN table.operation = '+' THEN cte.number + table.number
                     WHEN table.operation = '-' THEN cte.number - table.number END AS number, --etc.
    table.order FROM table INNER JOIN cte ON table.order = cte.order + 1 AND table.equation = cte.equation
            )
        SELECT equation, number, order 
        FROM cte
        OPTION (MAXRECURSION 1000);
    

    The first SELECT grabs your leftmost number, and the UNION all does the following operations on the number returned by it. The maxrecursion option limits the number of operations in one equation to 1000. You can, of course, set this higher.

    This answer is somewhat incomplete, because the final select query would return intermediate results. That’s fairly simple to filter though.

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

Sidebar

Related Questions

Let's say I have a bunch of classes that look something like... class Foo{
Let's say that I have a bunch of class instances that serve different purposes,
In C++, say you have a whole bunch of objects that you want to
Let's say I have some Control that has been disabled. It contains a bunch
Say I have a bunch of data on some people. This could include Name,
Say I have a LINQ-to-XML query that generates an anonymous type like this: var
Let's say I have a bunch of links that share a click event: <a
Let's say I have a bunch of well-known values, like this (but const char
Lets say we have a bunch of numbers that increment in small values from
Say you have a bunch of files. Say you can store meta data to

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.