简体   繁体   中英

How can i do this query by linq ,extensions methods or pure sql?

I have a table below

 first      second
-------     ----------
100        0
200        0
0           400

I want to get below result

 first      second      result
-------     ---------- ----------
100        0            100
200        0            300
0           400         -100

As you can see that result parameter is sum of previous (first-sum) How can i write such a query ?

Here's how to do it with LINQ (including setup):

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    public class Pair
    {
        public Pair(int first, int second)
        {
            this.First = first;
            this.Second = second;
        }

        public int First { get; set; }
        public int Second { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var pairs = new List<Pair>();
            pairs.Add(new Pair(100, 0));
            pairs.Add(new Pair(200, 0));
            pairs.Add(new Pair(0, 400));

            int currentTotal = 0;
            var runningTotals = pairs.Select(m => 
            { 
                currentTotal = currentTotal + (m.First - m.Second);

                return new
                {
                    First = m.First,
                    Second = m.Second,
                    Total = currentTotal
                };
            });

            foreach (var total in runningTotals)
            {
                Console.Write(total.First);
                Console.Write("\t\t");
                Console.Write(total.Second);
                Console.Write("\t\t");
                Console.Write(total.Total);
                Console.Write(Environment.NewLine);
            }

            Console.ReadLine();
        }
    }
}

Here's how to do it with SQL: http://www.databasejournal.com/features/mssql/article.php/3112381/SQL-Server-Calculating-Running-Totals-Subtotals-and-Grand-Total-Without-a-Cursor.htm

Hope that helps!

==UPDATE==

If you're using LINQ 2 SQL, you could pull in the records with something like this:

int currentTotal = 0;
var runningTotals = DataContextName.TableName.Select(m => 
{ 
    currentTotal = currentTotal + (m.First - m.Second);

    return new
    {
        First = m.First,
        Second = m.Second,
        Total = currentTotal
    };
});

This is a "running total" query. It can be done in SQL, but it depends on how the table is layed out.

Here's a SO Answer to this question on how to do it in SQL .

Here's an SO Answer on how to do this using Linq.

public class DataItem
{
    public int First { get; set; }
    public int Second { get; set; }
}

public static void Main(string[] args)
{
    List<DataItem> data = new List<DataItem>
        {
            new DataItem { First = 100, Second = 0 },
            new DataItem { First = 200, Second = 0 },
            new DataItem { First = 0, Second = 400 }
        };

    var result = data.Select((item, index) => new
        {
            First = item.First,
            Second = item.Second,
            Result = data.Take(index + 1).Sum(x => x.First - x.Second)
        });
}

mysql solution is very simple, but simple solutions are looking for mssql

set @result =0;
select first, second, @result := @result + first - second as result
from tablo; 

results

first  second  result  
100    0       100 
200    0       300 
0      400     -100 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM