I have to do a search to return a value , I have to do is the sum of multiplying two fields. I have the following code:
internal double TotalRes(long Id)
{
double total = 0;
Reserve rAlias = null;
var query = Session.QueryOver<Item>();
query = query.JoinAlias(e => e.Reserve, () => rAlias);
query = query.Where(() => rAlias.Id == Id);
query = query.Select(Projections.Sum<Item>(acct => acct.Ammount * acct.Wight));
object result = query.UnderlyingCriteria.UniqueResult();
if (result != null)
total = Convert.ToDouble(result);
return total;
}
It is giving the following error:
the variable 'acct' type 'tem' is referenced in scope '', but it is not set
How can i return this value?
The SQL multiply ( * ) operator is used to multiply two or more expressions or numbers.
SQL> with yourTable as 2 ( select 1 yourColumn from dual union all 3 select 2 from dual union all 4 select 4 from dual union all 5 select 8 from dual 6 ) 7 select EXP(SUM(LN(yourColumn))) As ColumnProduct from yourTable 8 / COLUMNPRODUCT ------------- 64 1 row selected.
SQL can also perform calculations and manipulate data through expressions. Expressions combine various SQL operators, functions, and values, to calculate a value. Mathematical expressions are commonly used to add, subtract, divide, and multiply numerical values.
Try to do something like this in the mapping, using formula.
Map(c => c.total).formula("(select sum(Ammount * Wight) from acct)");
Try this
Item rItem = null;
var query = Session.QueryOver<Item>(() => rItem);
...
query = query.Select(Projections.Sum(
Projections.SqlFunction(new VarArgsSQLFunction("(", "*", ")"),
NHibernateUtil.Double,
Projections.Property(() => rItem.Ammount),
Projections.Property(() => rItem.Wight))));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With