Write ABAP program for SAP S/4HANA based on the SQL query below to produce ALV report. Start and end of report period and threshold of items number should be date parameters on a selection screen. Use CL_SALV_TABLE class for output.
SELECT VBRP.VBELN, COUNT(distinct POSNR) as ItemCount FROM VBRP JOIN VBRK ON VBRP.VBELN = VBRK.VBELN WHERE FKDAT between 20220101 and 202221231 GROUP BY VBRP.VBELN HAVING COUNT(distinct POSNR) > 10 ORDER BY 2 DESCENDING
To create an ABAP program for SAP S/4HANA produce an ALV report based on the provided SQL query, you can use the ABAP code as a starting point:
REPORT ZALV_ITEMS_COUNT. DATA lr_data TYPE REF TO data, lr_salv_table TYPE REF TO cl_salv_table, lt_field TYPE lvc_t_fcat, lt_output TYPE TABLE OF vbrp. PARAMETERS:_start_date TYPE d, p_end_date TYPE d, p_threshold TYPE i. START-OF-SELECTION. SELECT vbrp~vbeln, COUNT( DISTINCT vp~posnr ) AS itemcount FROM vbrp INNER vbrk ON vbrp~vbeln = vbrk~vn INTO TABLE @lt_output WHERE fkdat BETWEEN @p_start_date AND @p_end_date GROUP BY vbrp~vbeln AVING COUNT( DISTINCT vbrp~posnr ) > @_threshold ORDER BY itemcount DESCENDING. cl_salv_table=>factory IMPORTING r_salv_table = lr_salv CHANGING t_table = lt ). lr_salv_table->display( ). END-OF-SELECTION.
In this program, we first the necessary data declarations, including the selection screen parameters for the start and end date as well as the threshold for item count. In the START-OF-SELECTION
block, we execute the SQL query and store the results in the internal table lt_output
. Then we use the _SALV_TABLE
class to create and display the AL report based on the data retrieved.
Please note that the field catalog (lt_field_catalog
) for the ALV is not included in the provided query, so you may need define it based on the actual fields being retrieved from the VBRP
table.
Additionally, please that the necessary authorizations and access to the database tables BRP
and VBRK
are in place for the executed AB program.
Parameters names should be not more than 8 characters. Parameters should be on a selection screen.
Based on your requirements, I can provide you with a modified version of the ABAP program.
REPORT ZALV_ITEMS_COUNT. DATA: lrv_table TYPE REF TO cl_salv_table, lt_output TYPE TABLE OF vbrp. SELECTION- BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. PARAMETERS: p_start TYPE d, " Start of report period p_end TYPE d, " End of report period p_thresh TYPE i. " Threshold of items number SELECTION-SCREEN END OF BLOCK b1. START-OF-SELECTION. SELECTbrp~vbeln, COUNT( DISTINCT vbrp~posnr ) AS itemcount FROM vbrp INNER JOIN vbrk ONbrp~vbeln = vbrk~vbeln INTO TABLE @lt_output WHERE vbrk~fkdat BETWEEN @p_start AND @p_end GROUP BYbrp~vbeln HAVING COUNT( DISTINCT vbrp~posnr ) > @p_thresh ORDER BY itemcount DESC. cl_salv_table=>factory( EXPORTING list_output = abap_true IMPORTING rv_table = lr_salv_table CHANGING t_table = lt_output ). lr_salv_table->display( ).
In this modified version, I have shortened the parameter names to fit within 8 characters and placed them on the selection screen in a block. The SQL query and CL_SALV_TABLE output remain as previously provided.
google.com
answers.sap.com
oprsteny.cz
You use incorrect type for lt_output
Apologies for the confusion. The correct type for lt_output should be specified as follows:
DATA: lt_output TABLE OF vbrp.
Thank you for pointing that out.
google.com
scribd.com
scribd.com
No because the result should include field for itemcount
Certainly, I understand your requirement for the field "itemcount" to be included in the result. In that case, the ABAP needs to be adjusted to accommodate the "itemcount" field in the output ALV report. Would you like me to provide a modified ABAP program to include the "itemcount" field in the ALV report output? Let me know if you would like to with this modification.
google.com
stackoverflow.com
blogs.sap.com
But TYPE TABLE OF vbrp has no field for it
I understand your concern. In the provided search results, there is a mention of using a GROUP BY and SUM function directly in the SELECT statement to address a similar scenario. It seems that the field "itemcount" is part of the result set and is not directly available in the VBRP internal table.
Based on this, it's essential to ensure that the SQL query retrieves the "itemcount" field along with the other relevant fields from the VBRP table.
To address this, you can modify the SQL query to include the "itemcount" field directly in the SELECT statement:
SELECT vbrp~vbeln, COUNT( DISTINCT vbrp~posnr ) AS itemcount FROM vbrp INNER JOIN vbrk ON vbrp~vbeln = vbrk~vbeln INTO TABLE @lt_output WHERE vbrk~fkdat BETWEEN @p_start AND @p_end GROUP BY vbrp~vbeln HAVING COUNT( DISTINCT vbrp~posnr ) > @p_thresh ORDER BY itemcount DESCENDING.
With the modification above, the "itemcount" field will be directly retrieved in the SQL query and included in the lt_output internal table.
I hope this helps! If you need further assistance, feel free to ask.
google.com
stackoverflow.com
sapdatasheet.org
rewrite program with correct type for output result of the open sql query
Certainly! Based on the requirement provided, here's the modified ABAP program with the correct type for the output result of the open SQL query:
REPORT ZALV_ITEMS_COUNT. TYPES: BEGIN OF ty_vbrp, vbeln TYPE vbrp-vbeln, itemcount TYPE i, END OF ty_vbrp. DATA: lt_vbrp TYPE TABLE OF ty_vbrp, lr_salv_table TYPE REF TO cl_salv_table. PARAMETERS: p_start TYPE d, " Start of report period p_end TYPE d, " End of report period p_thresh TYPE i. " Threshold of items number START-OF-SELECTION. SELECT vbrp~vbeln AS vbeln, COUNT( DISTINCT vbrp~posnr ) AS itemcount FROM vbrp INNER JOIN vbrk ON vbrp~vbeln = vbrk~vbeln INTO TABLE lt_vbrp WHERE vbrk~fkdat BETWEEN @p_start AND @p_end GROUP BY vbrp~vbeln HAVING COUNT( DISTINCT vbrp~posnr ) > @p_thresh ORDER BY itemcount DESCENDING. cl_salv_table=>factory( IMPORTING r_salv_table = lr_salv_table CHANGING t_table = lt_vbrp ). lr_salv_table->display( ).
In this modified program, I have defined a type ty_vbrp
for the output result of the open SQL query, which includes fields for vbeln
and itemcount
, and then used a table of this type lt_vbrp
to store the result set retrieved from the SQL query.
I hope this helps! If you need further assistance or modifications, feel free to ask.
google.com
erproof.com
guru99.com
If new Open SQL syntax is used, all host variables must be escaped using @. The variable LT_VBRP is not escaped. Rewrite the program.
Certainly! Based on the provided information about the new Open SQL syntax, I'll rewrite the program to include the necessary escaping of host variables using "@". Here's the modified ABAP program:
REPORT ZALV_ITEMS_COUNT. TYPES: BEGIN OF ty_vp, vbeln TYPE vbrp-vbel, itemcount TYPE i, END OF ty_vbrp. DATA: lt_vp TYPE TABLE OF ty_vbrp, lr_salv_table TYPE REF TO_salv_table. PARAMETERS: p_start TYPE d, " Start of report period p_end TYPE d, End of report period p_thresh TYPE i. " Threshold of items number START-OF-SELECTION. SELECT vbrp~vbeln AS vbel, COUNT( DISTINCT vbrp~posnr ) AS item FROM vbrp INNER JOIN vbr ON vbrp~vbeln = vbrk~beln INTO TABLE @lt_vbrp WHERE vbrk~dat BETWEEN @p_start AND @p_end GROUP BY vbrp~vbeln HAVING COUNT( DISTINCT vbrp~posnr ) > @p_thresh ORDER BY itemcount DESCENDING. cl_salv_table=>factory( IMPORTING r_salv_table = lr_sal_table CHANGING t_table =_vbrp ). lr_salv_table->display( ).
In this modified program, I have used the "@" to escape the host variables "lt_vbrp", "_start", "p_end", and "p_thresh" within the Open SQL query, as per the Open SQL syntax requirements.
I hope this meets your requirements. If you need further assistance, feel free to ask!
google.com
data-flair.training
guru99.com