Technical Articles
Filtering data in association with [n..*] cardinality
Introduction
One of the limitation with association in ABAP CDS view is we cannot use association field in where condition if the maximum cardinality of the association is n ( association[n..*] ).
Scenario
In the below example, a basic view on TFK003 (Document types) master data table is being created with association to its text table TFK003T. As the cardinality of the association is defined as [1..*], use of where condition on language field spras to restrict to session language results in below error
“To many” associations (cardinality [n..*]) are not allowed here
Alternative way is creating a new basic view for text table TFK003T with filter on language and using that new basic view for association which leads to extra effort.
To overcome this, we can use path expression on association name to filter out data as shown below:
Output:
Conclusion
The above approach is straight forward and will cut down the effort.
Note:
Similar approach can be used with any pair of tables with 1:n relationship to filter out data but care should be taken as the association with filter path expression can modify the cardinality of the results set.
Thanks Pruthvi. Helpful piece of information.
Hi, Pruthvi! Nice way for filtering, but why not filter directly on the declaration of the association?
Would you be able to tell if there's any advantage/disadvantage?
Thanks!
Hello Luiz,
That's a good point. we can use directly with association as well.
But the question would be similar to using filter on Join vs filter with Where clause in SQL.
Filtering with ON clause for may result in unexpected result while using LEFT, RIGHT or FULL OUTER JOIN. below link explains with example under heading Filtering data
https://dataschool.com/how-to-teach-people-sql/difference-between-where-and-on-in-sql/
We can use any approach as long as we vet the output to be correct.