web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Cartesian product of three tables via Query and QueryBuildDataSource

(0) ShareShare
ReportReport
Posted on by 1,650

Hello,

I need to connect three tables in a Query in order to get the cartesian product of all three in runtime. How can I achieve this requirenment using Query, QueryBuildDataSource and QueryRun. If I do it in the following matter the tables are fetched one after the other in the queryrun (first all values of "TableBase",  second all values of "Table1", third all values of "Table2"). The cursor "qr" never contains all the values at the same time.

Query                   queryAll  = new Query();

QueryBuildDataSource    qbds_base          = queryAll.addDataSource(tableNum(TableBase));


QueryBuildDataSource qbds_table1 = qbds_base.addDataSource(tableNum(Table1));
qbds_table1.joinMode(JoinMode::InnerJoin);

QueryBuildDataSource qbds_table2 = qbds_base.addDataSource(tableNum(Table2));
qbds_table2.joinMode(JoinMode::InnerJoin);

QueryRun qr = new QueryRun(queryAll);

while (qr.next())
{

TableBase tableBase = qr.get(tableNum(TableBase));

Table1 table1 = qr.get(tableNum(Table1));

Table2 table2 = qr.get(tableNum(Table2));

}

 

Thanks

Sebastian

I have the same question (0)
  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    Add relations (qbds.relations(true) or manuel relation)

  • Martin Dráb Profile Picture
    237,912 Most Valuable Professional on at

    Ergün is right - use relations(true) to use table relations, or addLink() if relations can't be used.

    By the way, please use Insert > Insert Code (in the rich formatting view) to paste source code. It's then easier to read. For example:

    Query                   queryAll  = new Query();
    QueryBuildDataSource    qbds_base  = queryAll.addDataSource(tableNum(TableBase));
    QueryBuildDataSource 	qbds_table1 = qbds_base.addDataSource(tableNum(Table1));
    qbds_table1.joinMode(JoinMode::InnerJoin);
    
    QueryBuildDataSource qbds_table2 = qbds_base.addDataSource(tableNum(Table2));
    qbds_table2.joinMode(JoinMode::InnerJoin);
    
    QueryRun qr = new QueryRun(queryAll);
    
    while (qr.next())
    {
    	TableBase tableBase = qr.get(tableNum(TableBase));
    	Table1 table1 = qr.get(tableNum(Table1));
    	Table2 table2 = qr.get(tableNum(Table2));
    }

  • Sebastian Aurand Profile Picture
    1,650 on at

    Thanks for the hints. I did not use "relations(true)" because the tables have no link. How could I establish a manual link in order to get all possible combinations in the query:

    Table Base:

    • ID1 = Entry1
    • ID2 = Entry2
    • ID3 = Entry3

    Table1:

    • IDa = First entry
    • IDb = Second entry
    • IDc = Third entry

    Table2:

    • No1 = Number 1
    • No2 = Number 2
    • No3 = Number 3

    Required result in the query:

    • ID1; IDa; No1
    • ID1; IDa; No2
    • ID1; IDa; No3
    • ID1; IDb; No1
    • ID1; IDb; No2
    • ID1; IDb; No3
    • ID1; IDc; No1
    • ...
    • ID3; IDc; No3

    In SQL this could be done with the select statement:

    SELECT *
    FROM TABLEBASE
    CROSS JOIN TABLE1
    CROSS JOIN TABLE2

    Sebastian

  • Verified answer
    Martin Dráb Profile Picture
    237,912 Most Valuable Professional on at

    Ah, sorry, I got distracted by the previous answer and didn't realize that your question is different.

    Then the fact that you don't have any join conditions is correct; that's what you want.

    What if you use qbds_table1.fetchMode(QueryFetchMode::One2One) and qbds_table2.fetchMode(QueryFetchMode::One2One)? Will you get the expected result?

  • Sebastian Aurand Profile Picture
    1,650 on at

    Thanks Martin. This was the missing point.

  • ergun sahin Profile Picture
    8,826 Moderator on at

    Sorry,  I missed that there is no relationship between tables. Since you didn't include the code with rich formatting, I gave my full attention to understanding the codes. It seems It was necessary to focus on what you wanted to convey with the "cartesian product".

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 559 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 464 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans