web
You’re offline. This is a read only version of the page.
close
Skip to main content
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)
  • Suggested answer
    Cody M Profile Picture
    55 on at
    RE: Select Count(Distinct field) using Group By

    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  ;
            }

  • Martin Dráb Profile Picture
    235,961 Most Valuable Professional on at
    RE: Select Count(Distinct field) using Group By

    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.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

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

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 789 User Group Leader

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 497 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans