Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
AmanSaxena
Participant
Introduction:

In the world of ABAP development, efficient data retrieval is crucial for optimal performance and seamless user experience. ABAP Core Data Services (CDS) views provide advanced techniques to leverage the power of the underlying database for efficient data retrieval and manipulation. In this blog post, we will explore various advanced data retrieval techniques with ABAP CDS views and provide a coding example to demonstrate their usage.


1. Joins and Associations:


ABAP CDS views offer the ability to fetch data from multiple tables by leveraging different types of joins and associations. With joins, you can combine data from related tables based on common fields. Associations, on the other hand, establish logical relationships between tables and allow for more intuitive navigation between entities. By utilizing joins and associations in CDS views, you can fetch data from multiple tables in a single query, reducing round trips to the database and improving performance.

Example:
@AbapCatalog.sqlViewName: 'ZVW_SALES_ORDER'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales Order CDS View'
define view ZSalesOrder as select from vbak
inner join vbap on vbap.vbeln = vbak.vbeln
{
key vbak.vbeln,
vbak.erdat,
vbak.kunnr,
vbap.matnr,
vbap.kwmeng
}
where vbak.erdat >= '2022-01-01'

 

2. Aggregations and Grouping:

CDS views enable the use of aggregate functions and grouping to perform calculations and summaries directly in the database layer. By utilizing aggregate functions like SUM, COUNT, AVG, and MAX along with grouping, you can retrieve aggregated data efficiently without the need for extensive post-processing in the application layer.

Example:
@AbapCatalog.sqlViewName: 'ZVW_SALES_ORDER_STATS'
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales Order Statistics CDS View'
define view ZSalesOrderStats as select from vbak
{
key vbak.kunnr,
count(*) as order_count,
sum(vbak.netwr) as total_amount
}
group by vbak.kunnr

 

3. Window Functions:

ABAP CDS views also support window functions, which enable advanced data analysis operations like ranking, partitioning, and cumulative calculations. Window functions operate on a set of rows and return a computed value for each row, allowing you to perform complex calculations efficiently within the database.

Example:
@AbapCatalog.sqlViewName: 'ZVW_EMPLOYEE_RANKING'
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Employee Ranking CDS View'
define view ZEmployeeRanking as select from employees
{
key employees.employee_id,
employees.first_name,
employees.last_name,
employees.salary,
rank() over (order by employees.salary desc) as ranking
}

4. Hierarchies and Recursive Queries:

CDS views offer capabilities to handle hierarchical data structures and perform recursive queries. With hierarchical CDS views, you can model parent-child relationships, such as organizational hierarchies or product category hierarchies, and easily navigate through the levels. Recursive queries allow you to retrieve data that is organized in a recursive manner, such as self-referential tables.

Example:
@AbapCatalog.sqlViewName: 'ZVW_ORG_HIERARCHY'
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Organization Hierarchy CDS View'
define view ZOrgHierarchy as select from org_structure
{
key org_structure.org_id,
org_structure.parent_org_id,
org_structure.org_name,
connect_by_root org_structure.org_id as top_org_id,
sys_connect_by_path(org_structure.org_name, '->') as org_path
}

Conclusion:

ABAP CDS views provide powerful capabilities for advanced data retrieval and manipulation. By leveraging techniques such as joins, associations, aggregations, window functions, and hierarchical queries, developers can optimize data access, enhance performance, and simplify complex data retrieval tasks. Incorporating these techniques into your ABAP development toolkit will empower you to create efficient and flexible data models that meet the needs of modern applications.

Remember to consult the SAP documentation and ABAP development guidelines for detailed information on syntax, usage, and best practices when working with ABAP CDS views.
1 Comment
Jelena
Active Contributor

Sorry but what makes any of this "advanced"?JOIN and COUNT/SUM (the way it's presented in this blog post) are basic SQL commands.

Also, classic CDS views that required DB view are already considered obsolete. Everyone who can is moving to CDS view entities, which work much better. They have been available since 2020.

Please use search to look for existing blog posts on SAP Community (there are already hundreds of them about CDS views) and educate yourself about latest features when writing such blog posts. Even if you wanted to show how to do something in an old release (not sure why, there are also old blogs about this from 3+ years ago), at least create a better example. Selecting from VBAK table directly and adding a date to WHERE in CDS is definitely not a way to go about this.

There is also this helpful CDS cheat sheet: https://www.brandeis.de/en/blog/cheat-sheet-cds-abap

Lots of information online, please do better research next time. It's the duty of every author, whether writing a blog or a book. It's really ironic you're asking the readers to consult documentation but it's apparent you haven't done that yourself.  ¯\_(ツ)_/¯

Thank you.