简体   繁体   中英

Equivalent of subselect from related table?

I'm trying to write the equivalent of the following SQL query in Axapta/X++, using the Query / QueryRun /etc. system:

SELECT FieldA,
       (SELECT SUM(FieldB) FROM TableB X WHERE X.FieldA = TableA.FieldA),
       (SELECT SUM(FieldB) FROM TableB Y WHERE Y.FieldA = TableA.FieldA),
       (SELECT SUM(FieldB) FROM TableB Z WHERE Z.FieldA = TableA.FieldA)
    FROM TableA

However, as far as I know, Axapta does not support subselect fields, so I can't write it directly as-is.

Is there some little-known feature or clever way to write this query other than using the database directly?

(I don't want to iterate over TableA and run the subqueries separately because that's how my report works now and it's too slow).

Thanks.

In this case I would use the display method modifier on TableA. Below is some pseudo-code, applied as a TableA buffer method.

display Amount getX()
{
    TableB tableB;
    ;
    select sum(FieldB) from tableB where tableB.FieldA == this.FieldA)
    return tableB.FieldB; 
}

As you can imagine, this approach is more costly than your SQL approach, so some datasource caching(scroll to bottom) can help.
Also, no sorting or filtering is possible on display methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM