Expressions on Aggregates?

Posts   
 
    
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 02-Feb-2007 21:10:46   

Is it possible to do the following as a scalar query:


SELECT
     SUM(Amount) - SUM(Other_Amount) NetAmount
FROM 
     SomeTable

I've read up on both aggregates and expressions, but I can't seem to put them together into an IEntityField2, which is what the GetScalar method needs.

Version 2, latest builds, Adapter vs. SQL Server.

Thanks,

Phil

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 03-Feb-2007 02:41:37   

How about something like this.

                IEntityField2 amount = SomeTableFields.Amount.SetAggregateFunction(AggregateFunction.Sum);
                IEntityField2 otherAmount = SomeTableFields.OtherAmount.SetAggregateFunction(AggregateFunction.Sum);
                IEntityField2 netAmount = SomeTableFields.Amount.SetFieldAlias("NetAmount");
                netAmount.ExpressionToApply = new Expression(amount, ExOp.Sub, otherAmount);

                using (DataAccessAdapter da = new DataAccessAdapter())
                {
                    ResultsetFields fields = new ResultsetFields(1);
                    fields.DefineField(netAmount, 0);
                    DataTable dynamicList = new DataTable();
                    adapter.FetchTypedList(fields, dynamicList, null);
                }
bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 03-Feb-2007 02:42:53   

I forgot the scalar part, let me take a look at that real quick.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 03-Feb-2007 02:48:54   

ok, try this instead.

                IEntityField2 amount = SomeTableFields.Amount.SetAggregateFunction(AggregateFunction.Sum);
                IEntityField2 otherAmount = SomeTableFields.OtherAmount.SetAggregateFunction(AggregateFunction.Sum);
                IEntityField2 netAmount = SomeTableFields.Amount.SetFieldAlias("NetAmount");
                netAmount.ExpressionToApply = new Expression(amount, ExOp.Sub, otherAmount);

                using (DataAccessAdapter da = new DataAccessAdapter())
                {
                    ResultsetFields fields = new ResultsetFields(1);
                    fields.DefineField(netAmount, 0);
                    int total = (int)adapter.GetScalar(fields, null, null);
                }
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 07-Feb-2007 16:23:51   

Finally got back around to trying this today. It worked, thanks!

Phil