COALESCE function in SAP CDS view

In SAP ABAP CDS (Core Data Services) View, COALESCEa function is a function for handling NULL values. COALESCEThe function accepts multiple parameters and returns the first non-NULL value parameter. This means that if the first parameter is not NULL, return the value of the first parameter; if the first parameter is NULL, return the value of the second parameter; and so on until the first non-NULL value is found parameters. Returns NULL if all arguments are NULL.

COALESCEFunctions are widely used in CDS View, and they can be used to deal with various complex data situations, such as default value setting, null value replacement and so on. In this article, we will detail the COALESCEuse of the function in CDS View and provide some practical examples to demonstrate its capabilities.

First, we need to understand COALESCEthe syntax of the function. In CDS View, COALESCEthe syntax of the function is as follows:

COALESCE( value1, value2, ..., valueN )

Among them, value1, value2, etc. are parameters, which can be field names, constant values ​​or expressions. CDS View will check the values ​​of the parameters in their order, returning the first non-NULL valued parameter. Returns NULL if all arguments are NULL.

Below we use several practical examples to illustrate COALESCEthe application of functions in CDS View in detail:

Example 1: Setting a default value using COALESCE

Suppose we have a CDS View for displaying employee information, including employee name and salary. Sometimes, the salary field may be empty, and we want to display it as a default value, such as 0, in the query results.

@AbapCatalog.sqlViewName: 'ZEMPLOYEES'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Employee Information'
define view ZEmployees as select from spfli
{
  key spfli.carrid,
  spfli.connid,
  spfli.cityfrom,
  spfli.cityto,
  spfli.countryfr,
  spfli.countryto,
  spfli.flighttype,
  spfli.distance,
  COALESCE(spfli.distance, 0) as distance_with_default
} 

In the above example, we use COALESCEthe function to set the default value of the distance_with_default field to 0. If the spfli.distance field is empty (that is, NULL), the distance_with_default field will display 0.

Example 2: Using COALESCE to replace NULL values

In some cases, we want to replace NULL values ​​with other specific values. For example, we have a CDS View that displays order information, which includes the order number and the date the order was created. If the order creation date is empty, we want to replace it with "N/A" in the query results.

@AbapCatalog.sqlViewName: 'ZORDERS'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Order Information'
define view ZOrders as select from vbak
{
  key vbak.vbeln,
  vbak.erdat,
  COALESCE(vbak.erdat, 'N/A') as erdat_replaced
} 

In the above example, we use COALESCEthe function to replace the NULL value of the erdat field with the string "N/A" and display the result in the erdat_replaced field.

Example 3: Complex judgments using COALESCE

COALESCEThe function can also be used for complex judgment logic, returning the first non-NULL value parameter that satisfies the condition. For example, we have a CDS View that displays information about a sales order, including the order number, order date, and order status. We hope that in the query results, if the order status is empty, the order status is judged as "completed" or "not completed" according to the order date.

@AbapCatalog.sqlViewName: 'ZSALESORDERS'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales Order Information'
define view ZSalesOrders as select from vbak
{
  key vbak.vbeln,
  vbak.erdat,
  vbak.status,
  COALESCE(vbak.status, CASE WHEN vbak.erdat <= '20230730' THEN '已完成' ELSE '未完成' END) as status_with_default
} 

In the above example, we use COALESCEthe function combined with the CASE expression to implement complex judgment logic. If the vbak.status field is empty, the order status will be judged as "completed" or "not completed" according to the order date vbak.erdat, and the result will be displayed in the status_with_default field.

Summarize

In SAP ABAP CDS View, COALESCEa function is a function for handling NULL values. It returns the first non-NULL value parameter, which can be used to set a default value, replace NULL values ​​or implement complex judgment logic. By using functions in CDS View COALESCE, we can process data more flexibly and improve the readability and usability of query results. In actual development, rational application of COALESCEfunctions can effectively optimize data processing and enhance system functions and user experience.

Guess you like

Origin blog.csdn.net/i042416/article/details/132018243