Friday, May 6, 2011

Query with SUM using SubSonic

Hi,

I'm new to Subsonic, I want to ask how to query with SUM?

I know how to query for where condition such as below:

Query qryCurOpcode = Station.CreateQuery()
    .WHERE("PRODLINE=PIECERATE_prodline")
    .AND("STATIONID=STNID")
    .AND("SHIFT=PIECERATE_shift");

IDataReader rdrCurOpcode = qryCurOpcode.ExecuteReader();

while (rdrCurOpcode.Read())
{
    PIECERATE_CurOpcode = rdrCurOpcode[Station.Columns.Curopcode].ToString();
}

but how to how to query with SUM?

Thanks! Halim

From stackoverflow
  • Here's one way of doing it:

    int sum = (int)new Query("Station").GetSum("Shift");
    

    You can get more examples from: SubSonic Aggregate Queries

  • If you would like to use an aggregate such as sum then the best way is to use Subsonic's aggregates :). For a whole list of examples check out SubSonicProject

    Here are some examples taken straight from the link above.

     double result = new
        Select(Aggregate.Avg("UnitPrice"))
        .From(Product.Schema)
        .ExecuteScalar<double>();
    
     IDataReader reader = new
        Select(Aggregate.GroupBy("ProductID"), Aggregate.Avg("UnitPrice"))
        .From("Order Details")
        .Where(Aggregate.Avg("UnitPrice"))
        .IsGreaterThan(50)
        .ExecuteReader();
    

0 comments:

Post a Comment