Tuning Dynamics NAV to the max: GROUP BY and DISTINCT

I know that this is not the first time that I write about this topic, but you can think at this as a “personal mission”: I want GROUP BY and DISTINCT on C/AL!!

If you’re a NAV developer and you know a bit of SQL, you can understand that a big lack on C/AL is the possibility to make a GROUP BY and DISTINCT query in an “atomic manner”. Let’s see a simple example (but that comes from a real case):

You’ve a table with data like these:

NAV_Distinct1

and you need to perform these tasks:

  1. group these data by the field called “Cod. Centro”
  2. calculate the sum of “Importo” for every “Cod. Centro”
  3. display the results on a form (“Cod. Centro” must be distinct and “Importo” must be the total).

If you work directly via T-SQL, you can perform this task simply with this query:

select distinct [Cod_ Centro], sum(Importo) as Total
from
[NAV$Mov_ Vis_ Analisi CoAn]
where [Codice Visualizzaz_ Analisi]='DRG'
group by [Cod_ Centro]

and you’ll obtain the result as follow:

NAV_Distinct2

If you work directly with NAV and C/AL, you’ve to write something like these:

//Raggruppo anche per VOCE
IF MovAnalisi.FINDSET(FALSE,FALSE) THEN
REPEAT
BEGIN
  IF MovAnalisi.MARK=FALSE THEN
  BEGIN
    //Save the primary key of the record that I want to summarize
    NumMov:=MovAnalisi.Nr;

    MovAnalisi2.COPYFILTERS(MovAnalisi);
    MovAnalisi2.SETRANGE("Dim. Voce",MovAnalisi."Dim. Voce");
    MovAnalisi2.SETRANGE("Val. Dim. Voce",MovAnalisi."Val. Dim. Voce");
    TotQta:=0;
    TotImporto:=0;
    IF MovAnalisi2.FINDSET THEN
    REPEAT
    BEGIN
      TotQta+=MovAnalisi2.Quantità;
      TotImporto+=MovAnalisi2.Importo;
      //Mark the record
      IF MovAnalisi.GET(MovAnalisi2.Nr) THEN
        MovAnalisi.MARK(TRUE);
    END
    UNTIL MovAnalisi2.NEXT=0;

    //Here you can write the summarized record

    //Re-position to the original record
    MovAnalisi.GET(NumMov);

END
UNTIL MovAnalisi.NEXT=0;

As you can see, you need two references to the same table, you need to mark the record to summarize etc. This is not the best in terms of performances…

I don’t think that is so impossible to add to C/AL something like RECORD.RETRIEVEDISTINCT(FieldName) or RECORD.GROUPBY(FieldName).

These are common operations on a production environment and they’re also many times a bottleneck in terms of performances (expecially if you work with big amount of data). Personally, I’ve to use ADO inside NAV in order to increase performances for these types of operations.

But why is so impossible to have them natively implemented on C/AL??? Help me on this mission… I want DISTINCT and GROUP BY on Rec!! :)

Technorati Tag:

Print | posted on Thursday, June 11, 2009 3:26 PM

Comments on this post

# re: Tuning Dynamics NAV to the max: GROUP BY and DISTINCT

Requesting Gravatar...
Great idea -how would this translate to non-SQL
Left by Ron Fox on Jun 11, 2009 5:13 PM

# re: Tuning Dynamics NAV to the max: GROUP BY and DISTINCT

Requesting Gravatar...
sorry, but this is a very stupit C/AL solution.
U need only one! record variable and only one! loop. Your doesn't need the unperform. mark function.

1. Recordvariable
2. A Buffertable that u use as temptable and store there the recs with the same filtercriteria and calc there the values.
Left by Old NAV developer on Jul 03, 2009 10:12 PM

# re: Tuning Dynamics NAV to the max: GROUP BY and DISTINCT

Requesting Gravatar...
I agree with you with the temporary table solution, but this is not always the way to do, expecially if you work (as most of my cases) with large (very lage) amounts of records.
A temporary table use the system memory to store its data, so it's extremely resource consuming. If clients has not enough memory (RAM) and enough disk space, sometimes this could be a problem (NAV shows very strange errors).
I've written a post about this in the past:
demiliani.com/blog/archive/2005/08/31/2894.aspx
Left by Stefano Demiliani on Jul 06, 2009 10:37 AM

# re: Tuning Dynamics NAV to the max: GROUP BY and DISTINCT

Requesting Gravatar...
Take a look at this for grouping (the last point): http://www.mibuso.com/howtoinfo.asp?FileID=22

BTW: the last point I added because I saw your example using marks. Using marks in SQL is not exactly a good idea for performance.
Left by Alain Krikilion on Jul 27, 2009 5:56 PM

# re: Tuning Dynamics NAV to the max: GROUP BY and DISTINCT

Requesting Gravatar...
I've received this response and I want to share it:
"Thank you for your suggestion. We do not plan add the suggested functionality; however, we are considering a new feature to support such functionality.
Best regards,
Merete Lydolph Larsen"
Left by Stefano Demiliani on Sep 02, 2009 3:57 PM

# re: Tuning Dynamics NAV to the max: GROUP BY and DISTINCT

Requesting Gravatar...
Hi, don't know if this is still active but anyways...
This is how i'd do it. Provided I were running under SQL i create a view with the SQL script and then just create a new table in NAV and link it to the view using linked object.

Left by Fredrik De Govia on Aug 19, 2010 12:15 PM

# re: Tuning Dynamics NAV to the max: GROUP BY and DISTINCT

Requesting Gravatar...
oh,good post,such useful information.
Left by tory burch on Aug 06, 2011 11:30 AM

Your comment:

 (will show your gravatar)
 
Please add 7 and 5 and type the answer here: