web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

addHavingFilter() method does not work with real

(2) ShareShare
ReportReport
Posted on by 104
I try to use addHavingFilter() but it does not work with real. By using Tracer I see that it transforms the real value in a non-sense int value and the query crashes with error "[Microsoft][ODBC Driver 17 for SQL Server]String data, right truncation".
Here is an example of the code:
 
public void handleIncludeHavingRestFilter(Query _q, QueryBuildDataSource _qbds, real value1)
{
    _q.clearHavingFilters();
	
	_q.addHavingFilter(
            _qbds,
            fieldStr(CustomView, Rest),
            AggregateFunction::Sum
            ).value(queryValue(value1));
    }
}
Categories:
I have the same question (0)
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,441 Super User 2025 Season 2 on at
     
    Are you sure that the “Rest” field is of type real ?
     
    Best regards,
    Mohamed Amine MAHMOUDI
  • Vasileios Papoglou Profile Picture
    104 on at
    Hi Mohamed,
     
    yes, it's a computed column of real type in a View.
  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at
    Please give us more details. Is _qbds a data source for CustomView? What do you have in value1? What do you get when you call _q.getSQLStatement() after running your logic?
     
    Ideally, prepare a simple piece of code using standard object, so anyone can run the code and see the behavior. It'll help you isolating the problem, you'll make it easier for other to help you and you'll need it anyway if you decide to log a support request.
  • Vasileios Papoglou Profile Picture
    104 on at
    Hello Martin,
     
    yes, the _qbds is datasource of the view. Here, I provide more details:
     
    public void handleIncludeHavingRestFilter1(Query _q, QueryBuildDataSource _qbds, real _value1)
    {
        //_value1 = 3.72 for example
        _q.clearHavingFilters();
    	
    	//this works
        str sqlS1 = _q.getSQLStatement();
        /*
            SELECT T1.ACCOUNTNUM,T1.ACCOUNTGROUP,T1.NAME,T1.VATNUM,T1.DATAAREAID,SUM(T2.DEBIT),SUM(T2.CREDIT),SUM(T2.REST),SUM(T2.PREVDEBIT),SUM(T2.PREVCREDIT),SUM(T2.PREVREST),SUM(T2.NEWDEBIT),SUM(T2.NEWCREDIT),SUM(T2.NEWREST),T2.ACCOUNTGROUP,T2.VATNUM 
            FROM  DCM_APR_CUSTVENDUNIONVIEW T1 
            LEFT OUTER JOIN DCM_APR_CUSTVENDTRANSBALUNIONVIEW T2 ON (((T2.PARTITION=5637144576) AND (T2.DATAAREAID IN (N'BIN') )) AND (((((((T2.RECTYPE=?) AND (T2.TRANSDATE<?)) OR ((T2.RECTYPE=?) AND ((T2.TRANSDATE>=?) AND (T2.TRANSDATE<=?)))) AND (T2.CUSTVENDRECTYPE=?)) AND (T1.ACCOUNTNUM=T2.ACCOUNTNUM AND (T1.DATAAREAID = T2.DATAAREAID) AND (T1.PARTITION = T2.PARTITION))) AND (T1.DATAAREAID=T2.DATAAREAID AND (T1.DATAAREAID = T2.DATAAREAID) AND (T1.PARTITION = T2.PARTITION))) AND (T1.CUSTVENDRECTYPE=T2.CUSTVENDRECTYPE AND (T1.DATAAREAID = T2.DATAAREAID) AND (T1.PARTITION = T2.PARTITION)))) 
            WHERE (((T1.PARTITION=5637144576) AND (T1.DATAAREAID IN (N'BIN') )) AND ((T1.CUSTVENDRECTYPE=?) AND ((((((((T1.ACCOUNTNUM=?) OR (T1.ACCOUNTNUM=?)) OR (T1.ACCOUNT
                        NUM=?)) OR (T1.ACCOUNTNUM=?)) OR (T1.ACCOUNTNUM=?)) OR (T1.ACCOUNTNUM=?)) OR (T1.ACCOUNTNUM=?)) OR (T1.ACCOUNTNUM=?)))) 
            GROUP BY T1.ACCOUNTNUM,T1.ACCOUNTGROUP,T2.ACCOUNTGROUP,T1.NAME,T1.VATNUM,T2.VATNUM,T1.DATAAREAID 
            ORDER BY T1.ACCOUNTNUM,T1.ACCOUNTGROUP,T2.ACCOUNTGROUP,T1.NAME,T1.VATNUM,T2.VATNUM,T1.DATAAREAID
        */
     
        _q.addHavingFilter(
            _qbds,
            fieldStr(DCM_APR_CustVendTransBalUnionView, Rest),
            AggregateFunction::Sum
            ).value(queryValue(_value1));
     
    	//this doesn't work
        str sqlS2 = _q.getSQLStatement();
     
        /*
            SELECT T1.ACCOUNTNUM,T1.ACCOUNTGROUP,T1.NAME,T1.VATNUM,T1.DATAAREAID,SUM(T2.DEBIT),SUM(T2.CREDIT),SUM(T2.REST),SUM(T2.PREVDEBIT),SUM(T2.PREVCREDIT),SUM(T2.PREVREST),SUM(T2.NEWDEBIT),SUM(T2.NEWCREDIT),SUM(T2.NEWREST),T2.ACCOUNTGROUP,T2.VATNUM 
            FROM DCM_APR_CUSTVENDUNIONVIEW T1 
            LEFT OUTER JOIN DCM_APR_CUSTVENDTRANSBALUNIONVIEW T2 ON (((T2.PARTITION=5637144576) AND (T2.DATAAREAID IN (N'BIN') )) AND (((((((T2.RECTYPE=?) AND (T2.TRANSDATE<?)) OR ((T2.RECTYPE=?) AND ((T2.TRANSDATE>=?) AND (T2.TRANSDATE<=?)))) AND (T2.CUSTVENDRECTYPE=?)) AND (T1.ACCOUNTNUM=T2.ACCOUNTNUM AND (T1.DATAAREAID = T2.DATAAREAID) AND (T1.PARTITION = T2.PARTITION))) AND (T1.DATAAREAID=T2.DATAAREAID AND (T1.DATAAREAID = T2.DATAAREAID) AND (T1.PARTITION = T2.PARTITION))) AND (T1.CUSTVENDRECTYPE=T2.CUSTVENDRECTYPE AND (T1.DATAAREAID = T2.DATAAREAID) AND (T1.PARTITION = T2.PARTITION)))) 
            WHERE (((T1.PARTITION=5637144576) AND (T1.DATAAREAID IN (N'BIN') )) AND ((T1.CUSTVENDRECTYPE=?) AND ((((((((T1.ACCOUNTNUM=?) OR (T1.ACCOUNTNUM=?)) OR (T1.ACCOUNTNUM=?)) OR (T1.ACCOUNTNUM=?)) OR (T1.ACCOUNTNUM=?)) OR (T1.ACCOUNTNUM=?)) OR (T1.ACCOUNTNUM=?)) OR (T1.ACCOUNTNUM=?)))) 
            GROUP BY T1.ACCOUNTNUM,T1.ACCOUNTGROUP,T2.ACCOUNTGROUP,T1.NAME,T1.VATNUM,T2.VATNUM,T1.DATAAREAID 
    		HAVING (SUM(T2.REST)=?) 
            ORDER BY T1.ACCOUNTNUM,T1.ACCOUNTGROUP,T2.ACCOUNTGROUP,T1.NAME,T1.VATNUM,T2.VATNUM,T1.DATAAREAID
        */
    
        //Database error: [Microsoft][ODBC Driver 17 for SQL Server]String data, right truncation
        //In Tracer I see the condition as: HAVING (SUM(T2.REST)=3014709)
    }
     
  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at
    Here is an example of simple code that anyone can run:
    Query query = new Query();
    QueryBuildDataSource ds = query.addDataSource(tableNum(Currency));
    
    ds.addSelectionField(fieldNum(Currency, RoundingPrecision), SelectionField::Sum);
    query.addHavingFilter(ds, fieldStr(Currency, RoundingPrecision), AggregateFunction::Sum).value(queryValue(1));
    
    QueryRun qr = new QueryRun(query);
    qr.next();
    This one doesn't throw the error, therefore we've just ruled out the idea that the HAVING filter doens't work with real-data-type fields.
    But there may be a problem with views or computed columns, or you have a bug in your customization, or your DB isn't correctly synchronized. You need more tests to find what is really to blame.
  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at
    It works with a real computed column as well. 
    Query query = new Query();
    QueryBuildDataSource ds = query.addDataSource(tableNum(PayPredOpenCustomerInvoicesView));
    
    ds.addSelectionField(fieldNum(PayPredOpenCustomerInvoicesView, CashDiscountPercent), SelectionField::Sum);
    query.addHavingFilter(ds, fieldStr(PayPredOpenCustomerInvoicesView, CashDiscountPercent), AggregateFunction::Sum).value(queryValue(4.2));
    
    QueryRun qr = new QueryRun(query);
    Your problem lies somewhere else.
     
    By the way, if I check the value of the having filter:
    Query query = new Query();
    query.literals(true);
    
    QueryBuildDataSource ds = query.addDataSource(tableNum(PayPredOpenCustomerInvoicesView));
    
    ds.addSelectionField(fieldNum(PayPredOpenCustomerInvoicesView, CashDiscountPercent), SelectionField::Sum);
    query.addHavingFilter(ds, fieldStr(PayPredOpenCustomerInvoicesView, CashDiscountPercent), AggregateFunction::Sum).value(queryValue(4.2));
    
    info(query.getSQLStatement());
    it looks good to me:
     
  • Vasileios Papoglou Profile Picture
    104 on at
    Well, your code is indeed a very good example and I used it. In my Currency table I have 2 records: EUR with RoundingPrecision = 0.01 and USD with RoundingPrecision = 0.
    When I create the query without Having filter I get 0.01 with Info(num2str(...)) which is correct.
    After that, I used the same query 2 times with Having filter, one with .value('> 0 ') and one with .value(queryValue(0.01)). Both returned 0.00.
    When using 0.01 the statement is HAVING(SUM(RoundingPrecision) = 1.E-2)).

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 456 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans