0

我遇到了两个问题:

我尝试将 ROW_NUMBER 分配给我的 SQL 视图并根据行号进行运行总和(因此根据每条记录获取总和),但由此产生的性能影响非常明显。

我知道如何在 T-SQL 中为日期执行此操作:

在此处输入图像描述

此 SQL 语句中的RunningTotal列正是我所需要的(逐项汇总)。

在此处输入图像描述

但是执行起来需要很长时间。

更新 2
这是我创建第二个视图的方式。

CREATE VIEW [dbo].[vCI_UNIONALL_ROW] AS
SELECT ROW_NUMBER() OVER(ORDER BY EffChkDt DESC) AS [Row]
    ,*
FROM vCI_UNIONALL
GO

我的模型:

public class UnionAllModel
{
    public int? InvoiceNumber { get; set; }
    [DataType(DataType.Currency)]
    public Decimal? Amount { get; set; }
    [DataType(DataType.DateTime)]
    public DateTime? EffChkDt { get; set; }
    [omitted for clarity]
}

到目前为止,这是我的 LINQ 查询:

using (var db = new PLOGITENS01Entities())
{
    var rvUnionAll = (from u in db.vCI_UNIONALL
                      orderby u.EffChkDt ascending
                      select new UnionAllModel()
                      {
                          InvoiceNumber = u.InvoiceNumber,
                          Date = u.Date,
                          AccountNumber = u.AccountNumber,
                          ClientName = u.ClientName,
                          Amount = u.Amount,
                          InAmount = u.InAmount,
                          OutAmount = u.OutAmount,
                          ClientRiskAdjs = u.ClientRiskAdjs,
                          SpecificAdjs = u.SpecificAdjs,
                          EffChkDt = u.EffChkDt,
                          PayStatus = u.PayStatus,
                          Type = u.Type
                      })
                     .ToList();

    return View(new GridModel(rvUnionAll));
}
4

1 回答 1

0

您可以尝试创建将值添加到局部变量的运行总和:

int runningSum = 0;
var rvUnionAll = (from u in db.vCI_UNIONALL.AsEnumerable()
                      orderby u.EffChkDt ascending
                      select new UnionAllModel()
                      {
                          InvoiceNumber = u.InvoiceNumber,
                          Date = u.Date,
                          AccountNumber = u.AccountNumber,
                          ClientName = u.ClientName,
                          Amount = u.Amount,
                          InAmount = u.InAmount,
                          OutAmount = u.OutAmount,
                          ClientRiskAdjs = u.ClientRiskAdjs,
                          SpecificAdjs = u.SpecificAdjs,
                          EffChkDt = u.EffChkDt,
                          PayStatus = u.PayStatus,
                          Type = u.Type,
                          RunningSum = runningSum += u.Amount
                      })
                     .ToList();
于 2021-05-12T10:23:53.267 回答