Select highest integer value

tompynation
Member Posts: 398
Imagine that you have a table with following fields:
Enabled Field No. Field Name Data Type Length Description
Yes 1 ID Integer
Yes 2 ReceptID Integer
Yes 3 Regelnr Integer
Yes 4 ReceptItem Text 50
Yes 5 Hoeveelheid Decimal
Yes 6 Percentage Decimal
Yes 7 ExtraOmschrijving Text 150
Yes 8 ToonAlsVolumeJN Boolean
Yes 9 ToonTussentotaalJN Boolean
I need a method inside this table which will return me the
highest integer value of the field Regelnr...
This function should receive a filter, namely the ReceptID
So for a given receptID i need the highest Regelnr
How do you do this?
Enabled Field No. Field Name Data Type Length Description
Yes 1 ID Integer
Yes 2 ReceptID Integer
Yes 3 Regelnr Integer
Yes 4 ReceptItem Text 50
Yes 5 Hoeveelheid Decimal
Yes 6 Percentage Decimal
Yes 7 ExtraOmschrijving Text 150
Yes 8 ToonAlsVolumeJN Boolean
Yes 9 ToonTussentotaalJN Boolean
I need a method inside this table which will return me the
highest integer value of the field Regelnr...
This function should receive a filter, namely the ReceptID
So for a given receptID i need the highest Regelnr
How do you do this?
0
Answers
-
Like so...
tableVar.RESET; //Sort by Regelnr (make sure this key exists) tableVar.SETCURRENTKEY(Regelnr); //Filter through only specified Receipt ID tableVar.SETFILTER(ReceiptID,'%1',specific_ReceiptID_var); //Find last record (highest integer) IF tableVar.FINDLAST THEN MESSAGE('The highest integer record for ReceiptID %1 is: %2!', tableVar.ReceiptID,tableVar.Regelnr);
0 -
This would be faster, but you need the index:
recTheTable.RESET; recTheTable.SETCURRENTKEY(ReceiptID,Regelnr); recTheTable.SETRANGE(ReceiptID,specific_ReceiptID_var); //Avoid SETFILTER when possible, but use SETRANGE IF recTheTable.FINDLAST THEN MESSAGE('The highest integer record for ReceiptID %1 is: %2!', recTheTable.ReceiptID,recTheTable.Regelnr);
If you don't have the index, but you have another index that starts with ReceiptID, you can also use that, but the code gets somewhat more complex, but you avoid a new index:intRegelnr := 0; recTheTable.RESET; recTheTable.SETCURRENTKEY(ReceiptID); //takes the first index that starts with "ReceiptID" recTheTable.SETRANGE(ReceiptID,specific_ReceiptID_var); //Avoid SETFILTER when possible, but use SETRANGE IF recTheTable.FINDSET THEN REPEAT IF intRegelnr < recTheTable.Regelnr THEN intRegelnr := recTheTable.Regelnr; UNTIL recTheTable.NEXT = 0; MESSAGE('The highest integer record for ReceiptID %1 is: %2!', recTheTable.ReceiptID,intRegelnr);
This last is slower in the reading then the first, but it avoids the overhead of an extra index that makes it slower for writing.Regards,Alain Krikilion
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!0 -
great works nice... thanks0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions