ERPClimb logoERPClimb
CDS Views
Architect / Cross-trackbeginner 8 min read

CDS Views Part 2: Joins, Expressions, CASE, CAST and Functions

Preview · a free account unlocks the full lesson

Key takeaway

Build CDS views that do real work in the database: inner and left outer joins, WHERE conditions, CASE expressions, CAST to ABAP types, string and date functions, and session variables such as the system date.

In this lessonShow sections

Visual overview

CDS Views Part 2: Joins, Expressions, CASE, CAST and Functions — learning sheetFlow: Create ZI_SalesOrderItemEnh: VBAP inner join VBAK → Add RejectionStatus with a searched CASE on ABGRU and CAST to abap.char(10) → Add SalesArea with concat and AgeInDays with dats_days_between and $session.system_date → Preview with F8, then read the view from a classic report sorted by AgeInDays. Key ideas: JOINS; WHERE IN THE VIEW; CASE; CAST AND ARITHMETIC. Watch out: Every calculation needs a stable result type — use CAST to fix length and type, especially in CASE branches that return literals of different lengths.ERPClimb.com / ARCH / LESSON 2/10 / LEARNING SHEETCDS Views Part 2: Joins, Expressions, CASE, CAST andFunctions1 · HOW IT FLOWS1CreateZI_SalesOrderItemEnhVBAP inner join VBAK2AddRejectionStatuswith a searched…3Add SalesAreawith concat andAgeInDays with…4Preview with F8,then read theview from a…2 · KEY IDEAS EXPLAINEDJoinsinner join: only rows with a match on both sides(order item + its header).left outer join: all rows from the left side,NULL/initial if no match (items with optionaltext).Where In The ViewA WHERE clause in the view is a fixed filter forevery consumer (e.g. only order type TA).Put only rules that are always true into the view;consumer-specific filters belong in the consumer'sSELECT.CaseSimple CASE: case field when 'A' then … else …end.Searched CASE: case when cond then … end — forranges and multiple fields.Cast And Arithmeticcast( field as abap.char(10) ), abap.int4,abap.dec(15,2), abap.fltp, or a data element(cast( x as vbeln_va )).Division of integers returns an integer — cast todecimal first, or use division( a, b, dec ).3 · REMEMBER · WATCH OUTRememberCDS supports inner join, left outer join, right outer join, cross joinand union — far beyond SE11 views.Calculated fields (CASE, CAST, arithmetic, string and date functions)run in the database, so ABAP receives finished values.Session variables ($session.system_date, $session.user,$session.system_language, $session.client) give runtime context…!Watch outEvery calculation needs a stableresult type — use CAST to fix lengthand type, especially in CASEbranches that return literals ofdifferent lengths.4 · IN A REAL PROJECTA backlog report for a pharma client read VBAP and VBAK in nested SELECTs, then calculated order age andstatus in ABAP loops. For 2 million items it ran 25 minutes. The team moved the join, the rejection statusCASE and the age calculation into one CDS view entity. The report became a single SELECT with a WHERE on…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Part 1 created a single-table view; now we combine tables and calculate values directly in the database. CDS supports inner join, left outer join, right outer join, cross join, and union — far beyond what a classic SE11 view could do.

An inner join returns only rows with a match on both sides, such as an order item joined to its header, while a left outer join returns all rows from the left side, filling in NULL or initial values where there's no match, useful for items with optional text.

Each data source should get its own alias (as item, as head), with fields prefixed accordingly, and join conditions should use = on key fields — functions in ON conditions hurt performance, as covered in Part 10. For relations you'll want to reuse, associations (covered in Part 6) are generally preferable to hard joins.

A WHERE clause written into the view itself becomes a fixed filter applied for every consumer — for example, restricting to order type TA. Only rules that are always true belong in the view; filters specific to one consumer belong in that consumer's own SELECT instead.

CASE expressions come in two forms: a simple CASE, like case field when 'A' then ... else ... end, and a searched CASE, like case when cond then ... end, which is better suited to ranges or conditions spanning multiple fields. Every calculation needs a stable result type, so literals in different CASE branches should be cast to the same type — a common source of errors is CASE branches returning literals of different lengths.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Referenced SAP objects

Reviewed reference pages for the objects this page mentions.

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.