0

我想将 Sid = 1 的所有 LineAmount 相加,如何在 SQLite 中执行?

SQL语句需要做什么?

await db.QueryAsync<Transaction>("Select * From Transaction Where Sid =1");

class TransactionLine
{
    [PrimaryKey, AutoIncrement]
    public int TId { get; set; }

    public int Sid { get; set; }
    public string No { get; set; }    
    public string Description { get; set; }

    public decimal Quantity { get; set; }

    public decimal UnitPrice { get; set; }
    public decimal LineDisPerc { get; set; }
    public decimal LineDiscAmt { get; set; }
    public decimal LineAmount { get; set; }

     public decimal FinalAmount { get; set; }

 }

- - 更新:

using (var db = new SQLite.SQLiteConnection(DBPath))
{
    var Orders = db.Query<TransactionLine>("Select *, Sum(FinalAmount) From TransactionLine where Sid ==" + OrderId);

    foreach (var lineTrans in Orders)
    {
        txtBlkAMT.Text = lineTrans.Amount.ToString();
    }          
}

问题:

  • 如果我有 3 行订单,则总结不正确。

----- 更新 2。

这有效:

但是在 SQL 语句中使用 Sum() 或其他函数有什么捷径吗?

十进制 LineTTL = 0; 十进制 LineAmt = 0; 整数计数=0;

var Orders = db.Query<TransactionLine>("Select * From TransactionLine where SId ==" + OrderId);                 

    foreach (var lineTrans in Orders)
     {

       LineAmt = lineTrans.Amount;

       LineTTL = LineTTL + LineAmt;

      TTLAmt = LineTTL;


      }
4

3 回答 3

0

确实有一个 SUM 函数:

SELECT SUM(Amount) FROM TransactionLine WHERE SId = 1
于 2013-10-28T09:56:53.833 回答
0
SELECT Sum(FinalAmount) AS Amount FROM TransactionLine WHERE Sid = 1

PS 使用“Transaction”作为表名不是一个好主意(保留字)。

于 2013-10-28T10:03:50.870 回答
-1

sqlite> SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME;

于 2013-10-28T03:11:07.303 回答