Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a multiplication in a SQL

Tags:

c#

queryover

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?

like image 894
Pedro Franco Avatar asked Aug 24 '15 13:08

Pedro Franco


People also ask

Is there a multiply function in SQL?

The SQL multiply ( * ) operator is used to multiply two or more expressions or numbers.

How do you write a multiplication aggregate function in SQL?

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.

Can you do math in SQL?

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.


2 Answers

Try to do something like this in the mapping, using formula.

Map(c => c.total).formula("(select sum(Ammount * Wight) from acct)");
like image 137
Rodrigo Martins Cirqueira Avatar answered Oct 07 '22 11:10

Rodrigo Martins Cirqueira


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))));
like image 21
romerotg Avatar answered Oct 07 '22 10:10

romerotg