GROUP BY query
slmaluwa
Member Posts: 366
I want to print a list like the one below from Cust. Ledger Entry table for a period.
Purpose is to print the no of unique customers the sales person touched (brought sales from) during that period.
I can't find a way to do it easily in NAV 4.0 SP3 - SQL
Pls help me to write a code to get this as my plan is to have a common "function" in a code unit.
Thank you
Maluwa
Purpose is to print the no of unique customers the sales person touched (brought sales from) during that period.
Salesperson NoOfUniqueCustomers AAA XXX BBB XXX
I can't find a way to do it easily in NAV 4.0 SP3 - SQL
Pls help me to write a code to get this as my plan is to have a common "function" in a code unit.
Thank you
Maluwa
"A bove maiore discit arare minor"-"From the old ox, the young one learns to plow."
0
Answers
-
If you just want to print it out you should investigate the GroupTotalFields property for Report data items. That way you can group by the Salesperson Code and easily display the number of customers.
If you want a function to do it you're going to have to sort the list by Salesperson and Customer and count them up every time the customer changes.
Counting the number of unique records is not one of NAV's strong areas.0 -
Hi
In NAV function "group by" in c/al like this just schematicCustLedgerEntry.reset; CustLedgerEntry.setcurrentkey("Salesperson Code","Customer No."); // need to add a key CustLedgerEntry.setfilter(...); // any addition filters if CustLedgerEntry.findset then repeat CustLedgerEntry.setrange("Salesperson Code",CustLedgerEntry."Salesperson Code"); TotalCustomers := 0; if CustLedgerEntry.findset then repeat CustLedgerEntry.setrange("Customer No.",CustLedgerEntry."Customer No."); TotalCustomers := TotalCustomers + 1; CustLedgerEntry.findlast; CustLedgerEntry.setrange("Customer No."); until CustLedgerEntry.next = 0; // keep a result in some table as example Result.init; Result."Salesperson Code" := CustLedgerEntry."Salesperson Code"; Result."Total Customers" := TotalCustomers; Result.insert; CustLedgerEntry.findlast; CustLedgerEntry.setrange("Salesperson Code"); until CustLedgerEntry.next = 0;
Best Regards0 -
if i have data like this
DATA01
DATA02
DATA01
DATA01
DATA02
DATA03
--> But i want to make like this
DATA01
DATA02
DATA03
how to do that?
like disctinct in sql syntax0 -
The Primary Key of Cust. Ledger Entry is "Entry No.". To sort by something different your report should use a more relevant key. Not to go into writing the whole report, you can make a key with MaintainSQLIndex = false with Salesperson Code,Customer No., or find another relevant key. First DataItem SalesPerson/Purchaser table, Second indented Cust. Ledger Entry Linked by Salesperson Code.
Then you can group Cust. Ledger entry on Customer No., and have summaries for each customer under the Salesperson section.-Lavin
"Profanity is the one language all programmers know best."0 -
bangswit wrote:if i have data like this
DATA01
DATA02
DATA01
DATA01
DATA02
DATA03
--> But i want to make like this
DATA01
DATA02
DATA03
how to do that?
like disctinct in sql syntaxrecData.RESET; recData.FINDSET; recData2 := recData; REPEAT reqUnique.SETRANGE(recData.Code); IF recUnique.ISEMPTY THEN BEGIN recData2.SETRANGE(recData.Code); IF recData2.FINDFIRST THEN BEGIN reqUnique := recData2; recUnique.INSERT; END END; UNTIL recData.NEXT = 0; IF NOT reqUnique.ISEMPTY THEN REPEAT MESSAGE(recUnique.Code); UNTIL reqUnique.NEXT = 0;Regards,
Andwian0 -
Andwian wrote:bangswit wrote:if i have data like this
DATA01
DATA02
DATA01
DATA01
DATA02
DATA03
--> But i want to make like this
DATA01
DATA02
DATA03
how to do that?
like disctinct in sql syntaxrecData.RESET; recData.FINDSET; recData2 := recData; REPEAT reqUnique.SETRANGE(recData.Code); IF recUnique.ISEMPTY THEN BEGIN recData2.SETRANGE(recData.Code); IF recData2.FINDFIRST THEN BEGIN reqUnique := recData2; recUnique.INSERT; END END; UNTIL recData.NEXT = 0; IF NOT reqUnique.ISEMPTY THEN REPEAT MESSAGE(recUnique.Code); UNTIL reqUnique.NEXT = 0;
thanks Andiwan
0 -
My pleasure, bang!bangswit wrote:thanks Andiwan

Anyway, is it works?
I have to admit that these codes are not so "efficient" though. You can try the suggested method by Yaroslav
.Yaroslav Gaponov wrote:Code: Select all
CustLedgerEntry.reset;
CustLedgerEntry.setcurrentkey("Salesperson Code","Customer No."); // need to add a key
CustLedgerEntry.setfilter(...); // any addition filters
if CustLedgerEntry.findset then
repeat
CustLedgerEntry.setrange("Salesperson Code",CustLedgerEntry."Salesperson Code");
TotalCustomers := 0;
if CustLedgerEntry.findset then
repeat
CustLedgerEntry.setrange("Customer No.",CustLedgerEntry."Customer No.");
TotalCustomers := TotalCustomers + 1;
CustLedgerEntry.findlast;
This code is more optimized to search for the only unique records, by always FINDLAST for the common records, but of course you have to set is as the current key.Regards,
Andwian0
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
- 323 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

