CDS

How to use division on quantity fields in CDS View

If you are trying to use CDS for data selection and calculation, chances are that you need to use division on quantity fields soon or later. It can be either for quantity conversion using factors or to calculate the ratio (percentage) wrt total quantity.

If you use divisor in the CDS, you will get an error that: Division x/y is only allowed for float numbers

This gives you the first hint, that the number needs to converted to float first. We know we need to use CAST to convert the data type of any field. It also means that probably you need separate CDS views now – first to change the data type and second for the actual division.

In this case, we are using conversion factors to change the quantity from order unit to base unit of measurement.

CDS 1: For CAST

define view ZPP_STOCK_EKET_OPENQUANTITY as select from ekko 
inner join ekpo
on ekko.ebeln = ekpo.ebeln
inner join eket
on ekpo.ebeln = eket.ebeln
and  ekpo.ebelp = eket.ebelp {
    
    key ekko.ebeln,
    key ekpo.ebelp,
    key eket.etenr,
    
        ekpo.matnr,
        ekpo.werks,
        ekpo.lgort,
        ekpo.meins,
        ekpo.lmein,

        cast(ekpo.umren as abap.fltp) as umren,
        cast(ekpo.umrez as abap.fltp) as umrez,
        cast(eket.menge as abap.fltp) as menge,
        cast(eket.wemng as abap.fltp) as wemng
}

CDS 2:

define view ZPP_STOCK_EKET_OPENQUANTITY_C as select from ZPP_STOCK_EKET_OPENQUANTITY
{
    
    key ebeln,
    key ebelp,
    key etenr,
    
        matnr,
        werks,
        lgort,

        meins,
        lmein,

                   case  
                   when meins = lmein then menge 
                   else (menge * umrez / umren )
                    end as menge,
                    case
                    when meins = lmein then wemng
                   else (wemng * umrez / umren )
                    end as wemng

}

Hope it helps. If like what we are doing here, please join our LinkedIn page and subscribe to our blog.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)

Follow by Email
Twitter
LinkedIn
LinkedIn
Share