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 :
Microsoft Dynamics AX (Archived)

How can I speed up and simplify query ?

(0) ShareShare
ReportReport
Posted on by 1,535

I have rows in Tab1:

MyName, MyValue, ...
"AAA", 127, ...
"AAA", 127, ...
"AAA", 133, ...
"AAA", 127, ...
"BBB", 400, ...
"BBB", 400, ...

MyName + MyValue have duplicates :-(

I want to count distinct pairs MyName+MyValue This is my query:

    while
    select  MyName, MyValue
    from    Tab1
    group by MyName, MyValue
    where       ....
    {
        counter++;
    }
    //in this example counter = 3

Is it possible to speedup and simplify query ?

*This post is locked for comments

I have the same question (0)
  • KyleLeBarre Profile Picture
    747 on at

    Adding an exists join would help filter out any records that do not have a duplicate record

    eg...

      while

       select  MyName, MyValue

       from    Tab1

       group by MyName, MyValue

       exists join Tab1Exists

       where Tab1Exists.MyName == Tab1.MyName

       && Tab1Exists.MyValue == Tab1.MyValue

       && Tab1Exists.RecId != Tab1.RecId

       where       ....

       {

           counter++;

       }

  • Suggested answer
    dolee Profile Picture
    11,279 on at

    Hi,

    You can send a TSQL query to get the result in X++ to reduce number of round trips. Create a new class and add the following method:

    public static server void Main(Args _args)

    {

       Connection  connection;

       Statement   statement;

       str         sql;

       ResultSet   resultSet;

       SqlStatementExecutePermission   perm;

       sql =  "SELECT COUNT(*) as [NumOfCombinations] FROM " +

               "( " +

               "select MyName, MyValue " +

               "from tab1 " +

               strFmt("where DATAAREAID = '%1' ",curext()) +

               "group by MyName, MyValue" +

               ") a";

       connection = new connection();

       perm = new SqlStatementExecutePermission(sql);

       perm.assert();

       statement = connection.createStatement();

       resultSet = statement.executeQuery(sql);

       while (resultSet.next())

       {

           info(strFmt("Total number of combinations: %1", resultSet.getInt(1)));

       }

       CodeAccessPermission::revertAssert();

    }

    And of cause you can just run the SQL in SSMS to get the result. :)

    Regards,

    Dominic

  • Dick Wenning Profile Picture
    8,705 Moderator on at

    add an index on the group by column values

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans