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, ...
Suggested Answer

Select Count(Distinct field) using Group By

(0) ShareShare
ReportReport
Posted on by 55

Hi All,

I'm trying to get the count of distinct quality order IDs in InventNonConformanceOrigin, where the quality order ID also exists in the InventQualityOrderTable:

        select count(RecId) from inventNonConformanceOrigin
            group by inventNonConformanceOrigin.InventQualityOrderId
            join inventQualityOrderTable
            where inventNonConformanceOrigin.InventQualityOrderId == inventQualityOrderTable.QualityOrderId;

The SQL query for it is:

SELECT COUNT(DISTINCT INVENTQUALITYORDERID) FROM INVENTNONCONFORMANCEORIGIN
INNER JOIN INVENTQUALITYORDERTABLE ON INVENTNONCONFORMANCEORIGIN.INVENTQUALITYORDERID = INVENTQUALITYORDERTABLE.QUALITYORDERID

The issue I'm have is that this just returns a value of 1 in inventNonConformanceOrigin.RecId. It should be returning over a thousand.

I have a similar while select query that selects distinct records using group by without any issues. It pulls all the records as expected:

        while select InventQualityOrderId from inventNonConformanceOrigin
            group by inventNonConformanceOrigin.InventQualityOrderId
            join inventQualityOrderTable
            where inventNonConformanceOrigin.InventQualityOrderId == inventQualityOrderTable.QualityOrderId
        {
            //
        }

Does anyone know what I'm missing with using the group by, or is there an alternate way to get the count I'm after? I've searched around and looked in this forum, but I wasn't able to find an existing post about this scenario. Any help is greatly appreciated, thank you!

I have the same question (0)
  • Martin Dráb Profile Picture
    237,924 Most Valuable Professional on at

    What you claim to be the SQL query for the X++ code is actually a completely different query. You forget to include the "GROUP BY" clause and that's also a reason why you get just a single value. Your SQL code counts all qualifying records; it doesn't count records per order ID.

    Also, another obvious difference between those two queries is that one counts distinct values only and the other counts all.

  • Suggested answer
    Cody M Profile Picture
    55 on at

    The SQL query doesn't have the group by because it has distinct, and I read that for x there was no distinct keyword, so it was recommended to use group by in x to get distinct records.

    I found another post after I posted that explained it better. I just ended up putting a counter in a while select, like mentioned in the responses to this post:
    https://community.dynamics.com/ax/f/microsoft-dynamics-ax-forum/151046/select-count-distinct-xx?pifragment-109373=2#responses

            while select RecId from inventNonConformanceOrigin
                group by inventNonConformanceOrigin.InventQualityOrderId
                join inventQualityOrderTable
                where inventNonConformanceOrigin.InventQualityOrderId == inventQualityOrderTable.QualityOrderId
            {
                counter  ;
            }

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