Usage of UNION operation in SAP CDS view

The following figure is the syntax and examples of SAP CDS view Union operations:

The SAP CDS (Core Data Services) view is an abstract and logical view of the SAP HANA database at the ABAP level. It not only provides a SQL-type language (Data Control Language, DCL) to query and manipulate data, but also provides various features and functions to support application development.

In SAP CDS, a Union operation is a way of merging or joining two or more CDS views. The purpose of this is to combine data from multiple data sources so that they can be used in a single query. The basic syntax of the Union operation is as follows:

SELECT FROM view1
UNION
SELECT FROM view2

In this example, view1 and view2 are the names of the CDS views. Union operations combine their result sets together. It should be noted that the Union operation requires that the two CDS views must have the same column structure, including the number of columns, the type of the columns, and the order of the columns.

As an example, let's assume we have two CDS views: one for SalesOrder and one for PurchaseOrder. The SalesOrder view contains all sales orders, and the PurchaseOrder view contains all purchase orders. Now, we want to query all orders, whether they are sales orders or purchase orders. We can do this using the Union operation:

@AbapCatalog.sqlViewName: 'ZALLORDERS'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'All Orders'
define view ZAllOrders as (
  select from ZSalesOrder
  union all
  select from ZPurchaseOrder
)

In this example, ZAllOrders is the newly defined CDS view that contains all sales and purchase orders. union allThe operation is a variant of the Union operation that preserves all results, including duplicates. If we only use union, then any duplicate results are eliminated.

Generally speaking, Union operations may be used in the following scenarios:

  1. Merge the results of multiple queries: If we have multiple queries, and each query returns the same structure of the result set, but the query conditions or query tables are different, then we can use the Union operation to merge the results of these queries. For example, we can use the Union operation to combine the results of querying employee information from two different departments.

  2. Remove duplicate records: In SQL, the Union operation will remove duplicate records in the result set by default. Therefore, if we want to remove duplicate records in the query results, we can use the Union operation.

  3. Query data from multiple tables: If we need to query data from multiple tables and merge the results together, we can use the Union operation. For example, we can use the Union operation to query product information under multiple product categories.

Next, let's look at an example of using Union operations. Suppose we have two tables, one is the sales table Sales, and the other is the purchase table Purchase. We want to query all sales and purchases and combine the results. Then, we can define a CDS View and use the Union operation to combine the results of querying Sales and Purchase:

@AbapCatalog.sqlViewName: 'ZCDS_SALES_PURCHASE'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales and Purchase View'
define view ZCdsSalesPurchase as select from sflight as sales
  union 
  select from scarr as purchase {
    key sales.carrid as Id,
    key sales.connid as ConnectionId,
    sales.fldate as FlightDate,
    sales.price as Price,
    'Sales' as Type
  } 
  union 
  select from scarr as purchase {
    purchase.carrid as Id,
    purchase.currcode as CurrencyCode,
    purchase.url as Url,
    purchase.mimetype as MimeType,
    'Purchase' as Type
  }

In this example, we first queried the Sales table and then combined the results from the Purchase table using a Union operation. In each query, we selected some fields and defined aliases for these fields to ensure that the structure of the two query results is the same. At the end, we added a Type field to identify whether the record is from the Sales or Purchase table.

Note that when using the Union operation, you need to ensure that the structure of all query results must be the same, that is, the number, order and type of fields selected by each query must be the same. In addition, the Union operation will remove duplicate records in the result set by default. If you do not want to remove duplicate records, you can use the Union All operation.

Finally, I want to emphasize that the Union operation does not change the original CDS view. It just creates a new view whose data is a collection of the original view's data.

Guess you like

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