如何编写将三列相加的SubSonic Select语句?

时间:2009-07-25 09:56:09

标签: subsonic

我必须使用SUM 3列并在结果中返回值。写这个的正确方法是什么?

SubSonic.SqlQuery qs =new SubSonic.Select()
.From(Table.ViewAllratingsGlo.Schema.TableName);

SubSonic.Aggregate ag = SubSonic.Aggregate.Sum(("Column1+Column2+Column3), "Score");
qs.Aggregates.Add(ag);

string SqlResult = qs.BuildSqlStatement();

SqlResult我只看到像

这样的聚合查询
Select SUM("Column1+Column2+Column3) As 'Score'

但我也需要我桌子上的其他字段。

1 个答案:

答案 0 :(得分:1)

在这种查询中,select语句中的所有列都需要是聚合对象,只需将聚合类型更改为group by而不是sum。

SubSonic.SqlQuery qs = new Select(
    new SubSonic.Aggregate("column1+column2+column3", "Score", AggregateFunction.Sum), //the original
    new SubSonic.Aggregate(TableName.Column4, AggregateFunction.GroupBy), //another column
    new SubSonic.Aggregate(TableName.Column5, AggregateFunction.GroupBy) //another column
)
.From(Table.ViewAllratingsGlo.Schema.TableName);