CDS Table function example to determine number of ... - SAP Community
source link: https://community.sap.com/t5/application-development-discussions/cds-table-function-example-to-determine-number-of-records/td-p/13677167
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Hi there,
currently I am on an example for a CDS table function to determine the number of records of a specific attribute.
Note: This is just an example for me to unterstand the functionality of such a CDS table function.
Please do not suggest different solutions like "for your use case you don't need a table function etc."
My use case is simplified!
Unfortunately, I don't get the solution on my own at the moment. Some parts I don't understand yet...
This is my use case:
Given database table:
ID |
id_1 |
id_1 |
id_1 |
id_2 |
id_2 |
id_3 |
Using the table function I want to get a result of the following structure:
ID | number |
id_1 | 3 |
id_2 | 2 |
id_3 | 1 |
So the number column shall only contain the count of occurrences of each ID.
This is what I got in coding yet:
CDS Table function:
@EndUserText.label: 'Test'
@ClientDependent: false
define table function MY_TABLE_FUNCTION
returns {
id : z_id;
number : z_number;
}
implemented by method zcl_table_function=>read;
AMDB class:
CLASS zcl_table_function DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb.
CLASS-METHODS read FOR TABLE FUNCTION my_table_function.
ENDCLASS.
CLASS zcl_table_function IMPLEMENTATION.
METHOD read BY DATABASE FUNCTION
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING my_table.
return select my_table.id from my_table as my_table;
ENDMETHOD.
ENDCLASS.
Class to call table function:
CLASS zcl_table_function_call DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
METHODS call.
ENDCLASS.
CLASS zcl_table_function_call IMPLEMENTATION.
METHOD call.
SELECT * FROM my_table_function( )
INTO TABLE (result).
ENDMETHOD.
ENDCLASS.
So. What now is still missing is the completion of the table function implementation itself.
How do I count the records there and build up the result table?
A prefered solution for me would be with the usage of temporary table variable in the table function to store the numbers.
Can anyone give me some hints?
BR
Daniel
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK