Discussion:
sum function in X++
(too old to reply)
Marien
2010-06-24 14:47:08 UTC
Permalink
Hello everyone!
Does enyone know if there is more elegant way to achieve SQL query below in
X++ then using the while loop?

select sum(Qty * amount) from OrderItem
where OrderItem.OrderNo = 'abc'

Thanks
Willy
2010-06-24 20:06:41 UTC
Permalink
Hi,

Take the following as an example:

server static void TestSumSelect()
{ Connection connection;
Statement statement;
str sql;
ResultSet resultSet;
SqlStatementExecutePermission perm;
;

connection = new Connection();
sql = strfmt( "SELECT SUM(QtyOrdered * SalesPrice) FROM SALESLINE where
SalesId='xyz'");
perm = new SqlStatementExecutePermission(sql);

perm.assert();
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);

while(resultSet.next())
{
info(num2str(resultSet.getReal(1),0,2,1,0));
}

CodeAccessPermission::revertAssert();
}


Code needs to run on server, put it in a class somewhere. (With args)
Good luck,


Willy (blog at http://dynamics-ax-live.blogspot.com)
Post by Marien
Hello everyone!
Does enyone know if there is more elegant way to achieve SQL query below in
X++ then using the while loop?
select sum(Qty * amount) from OrderItem
where OrderItem.OrderNo = 'abc'
Thanks
Luegisdorf
2010-06-25 07:50:35 UTC
Permalink
select sum(Qty), sum(amount) from OrderItem
where OrderItem.OrderNo = 'abc'

x = orderItem.Qty * orderItem.Amount

:)
Post by Marien
Hello everyone!
Does enyone know if there is more elegant way to achieve SQL query below in
X++ then using the while loop?
select sum(Qty * amount) from OrderItem
where OrderItem.OrderNo = 'abc'
Thanks
unknown
2010-06-27 12:35:49 UTC
Permalink
Post by Luegisdorf
x = orderItem.Qty * orderItem.Amount
For example, if you have OrderItem like this:
Qty Amount
1 100
2 10

Then 3*(110) != (100 + 2*10).
Okey, lets say that smile at the end of the message was placed there with
purpose ;)

I think in this case is easier to introduce new table field, which should be
filled with (Qty*Amount) on insertion, rather than use direct SQL statements.
Otherwise use while loop.
--
Regards,
Alex
----
Microsoft Dynamics AX Add-on for developers(Imptoved IntellySense,
additional hotkeys etc) -http://www.axassist.com
Post by Luegisdorf
select sum(Qty), sum(amount) from OrderItem
where OrderItem.OrderNo = 'abc'
x = orderItem.Qty * orderItem.Amount
:)
Post by Marien
Hello everyone!
Does enyone know if there is more elegant way to achieve SQL query below in
X++ then using the while loop?
select sum(Qty * amount) from OrderItem
where OrderItem.OrderNo = 'abc'
Thanks
Marien
2010-06-28 08:02:59 UTC
Permalink
Thank you all!
I needed this for a display method of datasource on a form, so I used
while loop and cashed this method. It works fine enough:)
Post by unknown
Post by Luegisdorf
x = orderItem.Qty * orderItem.Amount
Qty Amount
1 100
2 10
Then 3*(110) != (100 + 2*10).
Okey, lets say that smile at the end of the message was placed there with
purpose ;)
I think in this case is easier to introduce new table field, which should be
filled with (Qty*Amount) on insertion, rather than use direct SQL statements.
Otherwise use while loop.
--
Regards,
Alex
----
Microsoft Dynamics AX Add-on for developers(Imptoved IntellySense,
additional hotkeys etc) -http://www.axassist.com
Post by Luegisdorf
select sum(Qty), sum(amount) from OrderItem
where OrderItem.OrderNo = 'abc'
x = orderItem.Qty * orderItem.Amount
:)
Post by Marien
Hello everyone!
Does enyone know if there is more elegant way to achieve SQL query below in
X++ then using the while loop?
select sum(Qty * amount) from OrderItem
where OrderItem.OrderNo = 'abc'
Thanks
Pranjal Shukla
2021-03-22 06:47:14 UTC
Permalink
you can try this display method::
https://d365code.blogspot.com/2021/03/return-sum-of-any-table-field-using-x.html
Loading...