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:
@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;
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.