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

Announcements

No record found.

News and Announcements icon
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
    239,022 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 617

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 461 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 298 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans