Use temporary variable to distinct or group by element to fetch record in Axapta

Fetch method is override method of report in axapta. You can Use temporary variable to distinct or group by element to fetch record in Axapta .

By this example you can get only hints or idea to use in your report.

 public boolean fetch()  
 {  
 Query q = new Query();  
 QueryBuildDataSource qbds;  
 ;  
 q= this.query();  
 qbds = q.dataSourceTable(tablenum(LedgerTrans));  
 // qbds = q.addDataSource()  
 qbds.addSortField(fieldnum(LedgerTrans,AccountNum));  
 rangeTransDate =qbds.findRange(fieldnum(LedgerTrans,TransDate));  
 purpose1 = qbds.findRange(FieldId2Ext(FieldNum(LedgerTrans, Dimension),3));  
 if(!rangeTransDate.value())  
 {  
 rangeTransDate.value(queryRange(dateMin,dateMax));  
 fromdate= dateMin;  
 todate =dateMax;  
 }  
 else  
 {  
 value=rangeTransDate.value();  
 if(strfind(value,".",1,11) != 0)  
 {  
 mindate1 =strdel(value,strfind(value,".",1,11),strlen(value));  
 maxdate1 =strDel(value,strfind(value,".",1,11)+1,-(strfind(value,".",1,11)+1));  
 dateMin = str2date(mindate1,123);  
 dateMax= str2date(maxdate1,123);  
 fromdate= dateMin;  
 todate =dateMax;  
 }  
 else  
 {  
 fromdate =str2date(value,123);  
 todate = str2date(value,123);  
 }  
 }  
 queryRun = new QueryRun(q);  
 tmpAC = "";  
 while(queryRun.next())  
 {  
 LedgerTrans1 = queryRun.get(tablenum(LedgerTrans));  
 if(tmpAC != LedgerTrans1.AccountNum)  
 {  
 if(tmpAC != "")  
 {  
 element.FinalTOTAL();  
 ProgrammableSection.executeSection();  
 element.defaultSetting();  
 }  
 }  
 LedgerAC = LedgerTrans1.AccountNum;  
 Accountnum = LedgerTable::find(LedgerTrans1.AccountNum).AccountName;  
 if(LedgerTrans1.AmountMST < 0)  
  {  
  Credit += LedgerTrans1.AmountMST * -1;  
 }  
 else  
 {  
  Debit += LedgerTrans1.AmountMST;  
  }  
  tmpAC = LedgerTrans1.AccountNum;  
 }  
 if(tmpAC != "")  
 {  
 element.FinalTOTAL();  
 ProgrammableSection.executeSection(); element.defaultSetting();  
 }  
 ProgrammableSection_1.executeSection();  
  return true;  
  }