Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

AOT Query Groupby and Order by

Posted on by 434

Hi,

I need to know about the AOT query and how the Groupby is working in it. 

pastedimage1581329540766v1.png

Here is my Query i have created the relation manually For Salestable.salesid==salesline.salesid, salestable.custaccount== custtable,accountnum. when i use group by for Salestable.custaccount only the data for the other fields in the grid is not populating. when i use groupby for all the fields the data are populated on the grid, but it was not worked properly The custaccount field shows duplicate data after using Groupby . How the Groupby works and still i dont know much about that. 

pastedimage1581329810616v2.png

Regards,

Ram.

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: AOT Query Groupby and Order by

    It's not possible. If you grouped by CustAccount only, you would get one record per CustAccount (and no value in SalesId). That's by definition. If you get repeated CustAccount, you're not grouping by CustAccount only.

    You must use aggregate functions for fields that you don't group by but you want to get values for them. Please read again the explanation and examples above.

  • Ram Kumar Profile Picture
    Ram Kumar 434 on at
    RE: AOT Query Groupby and Order by

    Hi Martin,

    Forget About the Other Three fields Let us Consider CustAccount and Salesid Field only. I have used SalesTable.CustAccount For Group by but it shows Multiple records in the CustAccount Field. Is this necessary to use Aggregate function in Groupby ?

    Regards,

    Ram.

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: AOT Query Groupby and Order by

    Yes, that's the expected behavior. As I said in my example, if you group by both CustAccount and Unit, you'll get one record for each combination of CustAccount and Unit. If a customer (Cust1) have orders with three units (e.g. Piece, Meter and Box), you'll get three records (Cust1-Piece, Cust1-Meter and Cust1-Box). If you don't want that, don't do that. If you want to group by CustAccount only, don't group by other fields.

    But then you must decide how you want to aggregate the data. You can't show all three units in the single field - as I mentioned, you must use on of the aggregate functions. With units, there is probably no meaningful aggregation, but I gave you three different examples in my previous reply.

  • Ram Kumar Profile Picture
    Ram Kumar 434 on at
    RE: AOT Query Groupby and Order by

    Hi Martin,

    Yeah I have added the other fields also in the Group by. But it gives me the Repeated data for the CustAccount field. It gives me the Data For each Salesid. Here see the below image. It was grouped by salesid only. But i need CustAccount Should be Grouped. 

    pastedimage1581333334388v1.png

    Regards,

    Ram.

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: AOT Query Groupby and Order by

    If you group records by CustAccount, you'll get just a single record for each customer. You won't get anything for other fields, such as Unit, because there may be several different units used on orders of this customer. If you want to get a value, you must either include it in grouping (then you'll get one record for each Customer-Unit combination), or you must use an aggregate function. For example, you can use SUM to summarize amounts, COUNT to count the number of lines and MAXOF to get the highest status.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans