如何在SQLite中使用SQL语句进行sum和other等计算

时间:2013-10-28 02:35:06

标签: sqlite winrt-xaml

我想把所有LineAmount加起来,其中Sid = 1,如何在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;      decimal LineAmt = 0;      int Count = 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;


      }

3 个答案:

答案 0 :(得分:0)

确实存在SUM函数:

SELECT SUM(Amount) FROM TransactionLine WHERE SId = 1

答案 1 :(得分:0)

SELECT Sum(FinalAmount) AS Amount FROM TransactionLine WHERE Sid = 1

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

答案 2 :(得分:-1)

源码&GT;按名称从公司组中选择名称,SUM(SALARY);