Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sum function in X++

4,423 views
Skip to first unread message

Marien

unread,
Jun 24, 2010, 10:47:08 AM6/24/10
to
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

unread,
Jun 24, 2010, 4:06:41 PM6/24/10
to
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)

Luegisdorf

unread,
Jun 25, 2010, 3:50:35 AM6/25/10
to
select sum(Qty), sum(amount) from OrderItem
where OrderItem.OrderNo = 'abc'

x = orderItem.Qty * orderItem.Amount

:)

Alex K. at dot

unread,
Jun 27, 2010, 8:35:49 AM6/27/10
to
This will not work:

> 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

Marien

unread,
Jun 28, 2010, 4:02:59 AM6/28/10
to
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:)

Pranjal Shukla

unread,
Mar 22, 2021, 2:47:15 AM3/22/21
to
0 new messages