Sum table values

Asked

Viewed 707 times

3

I have a table where I return the amounts paid per taxpayer monthly. But I need to create a subtotal field, adding up the values of each month. Very similar to the Fibonacci sequence. The problem is that I need to look up when the taxpayer made the payment. In this part I’m winding up.

The data I currently have:

Dados Atual

How I need the data to return:

Dados Finais

I tried something like:

 double a = 0;
        double AnoContribuinte = 0;

        for (int i = 0; i < 10; i++)
        {
            a = a + Contribuinte;
            i++;
            Console.WriteLine(a);
        }

Only then I limited it to i< 10, and in fact I need I to be equal to the amount of data that the table has.

But in this way he enters a loop infinite, for I will always have some value.

  • you want the bank to return a View with this information? or this Subtotal field exists only in your application Model?

  • Apparently, I will never be null and so your being will never end. Another thing, if you add the subtotal this way, it at the end of the for will have the last value assigned. The ideal is to take the Count from the first table and do the FOR for for it and for each value added to the subtotal, you already download in the grid, by the understood, or save in a list and then take the grid and assign this list to the datasource of the grid and bindar it.

  • @Tobymosque this Subtotal field only exists in the Application.

  • 2

    Where does that come from Contribuinte? It does not vary. Where is your table? This excerpt and past information give no indication of how to solve the problem.

  • @Renilsonandrade, I’d like to give you some advice that escapes your doubt a little. avoid declaring variables as a, always try to use something that better describes the variable itself, such as subTotal.

3 answers

6


I see some problems in this code:

  1. The code appears to be manipulated monetary values and using the type double, this does not give hit, you will have rounding problems.
  2. Is increasing i twice, I can’t imagine why this is necessary.
  3. The data being taken does not vary.

What you can help with is this:

var listaContribuintes = PegaContribuintes(); // este método traz os contribuintes de algum lugar
Decimal subTotal = 0;

foreach (var item in listaContribuintes) {
    subTotal += item.Contribuinte;
    item.SubTotal = subTotal;
} 

Code:

using static System.Console;
using System.Collections.Generic;
                    
public class Program {
    public static void Main() {
        //as primeiras linhas abaixo é o mesmo que seria o método PegaContribuintes que eu usei no exemplo da resposta
        var pessoas = new List<Pessoa> { 
            new Pessoa { Nome = "Elias Vieira1", Contribuinte = 31.87m },
            new Pessoa { Nome = "Elias Vieira2", Contribuinte = 32.87m },
            new Pessoa { Nome = "Elias Vieira3", Contribuinte = 33.87m },
            new Pessoa { Nome = "Elias Vieira4", Contribuinte = 34.87m }
        };
        decimal subTotal = 0;
        foreach (var item in pessoas) {
            subTotal += item.Contribuinte;
            item.SubTotal = subTotal;
            WriteLine($"Nome: {item.Nome}, Contribuição: {item.Contribuinte}, SubTotal: {item.SubTotal}");
        }
    }
}

public class Pessoa {
    public string Nome { get; set; }
    public decimal Contribuinte { get; set; }
    public decimal SubTotal { get; set; }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference. You have to adapt to your list, be as it is composed.

If you have more details I will update the reply.

  • I’m sorry I didn’t post the necessary data, but I didn’t even know what my problem really was, so I posted a simplified version. I will elaborate a more elaborate question, about my real problem. Your code helped me to notice what my problem, thank you very much.

3

So let’s assume the following model:

public class Pessoa
{
    public string Nome { get; set; }
    public double Contribuinte { get; set; }
    public double SubTotal { get; set; }
}

And we have the following list, in the example below is being statically loaded:

var pessoas = new List<Pessoa>();
pessoas.Add(new Pessoa { Nome = "Elias Vieira", Contribuinte = 31.87d });
pessoas.Add(new Pessoa { Nome = "Elias Vieira", Contribuinte = 31.87d });
pessoas.Add(new Pessoa { Nome = "Elias Vieira", Contribuinte = 31.87d });
pessoas.Add(new Pessoa { Nome = "Elias Vieira", Contribuinte = 31.87d });

You can browse as follows:

var subTotal = 0.0d;
foreach(var pessoa in pessoas)
{
    subTotal += pessoa.Contribuinte;
    pessoa.SubTotal = subTotal;
}
  • I’m sorry I didn’t post the necessary data, but I didn’t even know what my problem really was, so I posted a simplified version. I will elaborate a more elaborate question, about my real problem. Your code helped me to notice what my problem, thank you very much.

  • I marked the @Bigown response as the right one, as it has an example that can help other people who have a similar question.

0

  • 1

    the cost to perform a subquery in SELECT tends to be high, especially when the query returns many records. In this specific case the ideal is to use a combination of CTE, Temporary Tables or even cursors. read more on: http://sqlperformance.com/2012/07/t-sql-queries/running-totals

  • Very good article Toby, reinforces the point that this kind of calculation is best done in the bank and not in the code!

  • I only have access to View that returns this data. And this specific field already uses a SUM() from another table. Ex: select SUM(table.field) as Contributor... I don’t know if I can solve the problem in the appeals field with the Field Alias, in creating a View.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.