SAP ABAP: COLLECT STATEMENT
Hace unas horas acabo de recibir un correo de un amigo nuestro consultando sobre como hacer lo siguiente en ABAP.
CODIGO FUENTE:
*&---------------------------------------------------------------------*
*& DECLARATION INTERNAL TABLES
*&---------------------------------------------------------------------*
data: lt_vbrk type standard table of lty_vbrk,"Todos los meses
lt_vbrk_1 type standard table of lty_vbrk,"Para el Mes 1
lt_vbrk_2 type standard table of lty_vbrk,"Para el Mes 2
lt_vbrk_3 type standard table of lty_vbrk."Para el Mes 3.
*&---------------------------------------------------------------------*
*& DECLARATION FIELD-SYMBOLS
*&---------------------------------------------------------------------*
field-symbols: <vbrk> like line of lt_vbrk.
*&---------------------------------------------------------------------*
*& Form START-OF-SELECTION
Bienvenido las nuevas sugerencias y/o propuestas para resolver este caso!
Nos vemos en el siguiente Blog :)
MAGALEX
SELECT
CLIENTE,
ENE = SUM (CASE WHEN MONTH (FECHA) = 1 THEN IMPORTE
ELSE 0 END )
FEB = SUM (CASE WHEN MONTH (FECHA) = 2 THEN IMPORTE
ELSE 0 END )
MAR = SUM (CASE WHEN MONTH (FECHA) = 3 THEN IMPORTE
ELSE 0 END ).
Y como todo caso puede ser resuelto por N formas, aqui va una sugerencia y/o propuesta para dar solución. Veamos:
*&---------------------------------------------------------------------*
*& DECLARATION GLOBAL TYPES
*&---------------------------------------------------------------------*
types: begin of lty_vbrk,
fkdat type fkdat,
mwsbk type mwsbp,
end of lty_vbrk.
*& DECLARATION GLOBAL TYPES
*&---------------------------------------------------------------------*
types: begin of lty_vbrk,
fkdat type fkdat,
mwsbk type mwsbp,
end of lty_vbrk.
*&---------------------------------------------------------------------*
*& DECLARATION INTERNAL TABLES
*&---------------------------------------------------------------------*
data: lt_vbrk type standard table of lty_vbrk,"Todos los meses
lt_vbrk_1 type standard table of lty_vbrk,"Para el Mes 1
lt_vbrk_2 type standard table of lty_vbrk,"Para el Mes 2
lt_vbrk_3 type standard table of lty_vbrk."Para el Mes 3.
*&---------------------------------------------------------------------*
*& DECLARATION FIELD-SYMBOLS
*&---------------------------------------------------------------------*
field-symbols: <vbrk> like line of lt_vbrk.
*&---------------------------------------------------------------------*
*& Form START-OF-SELECTION
*&---------------------------------------------------------------------*
start-of-selection.
perform set_data.
perform process_data.
*&---------------------------------------------------------------------*
*& Form SET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form set_data .
"En si haríamos una consulta como la siguiente:
* select fkdat mwsbk
* into table lt_vbrk
* from vbrk.
"pero para efectos de prueba haremos lo sgte:
"JANUARY
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130125'.
<vbrk>-mwsbk = 100.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130126'.
<vbrk>-mwsbk = 300.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130127'.
<vbrk>-mwsbk = 500.
"FEBRUARY
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130225'.
<vbrk>-mwsbk = 200.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130226'.
<vbrk>-mwsbk = 400.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130227'.
<vbrk>-mwsbk = 600.
"MARCH
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130325'.
<vbrk>-mwsbk = 700.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130326'.
<vbrk>-mwsbk = 800.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130327'.
<vbrk>-mwsbk = 900.
endform. " SET_DATA
*&---------------------------------------------------------------------*
*& Form PROCESS_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form process_data .
data: lv_month type char2,
lv_mes type i.
loop at lt_vbrk assigning <vbrk>.
lv_month = <vbrk>-fkdat+4(2).
move lv_month to lv_mes.
clear <vbrk>-fkdat.
case lv_mes.
when 1.
collect <vbrk> into lt_vbrk_1.
when 2.
collect <vbrk> into lt_vbrk_2.
when 3.
collect <vbrk> into lt_vbrk_3.
endcase.
endloop.
endform. " PROCESS_DATA
EJECUTANDO & DEBUGEANDO
start-of-selection.
perform set_data.
perform process_data.
*&---------------------------------------------------------------------*
*& Form SET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form set_data .
"En si haríamos una consulta como la siguiente:
* select fkdat mwsbk
* into table lt_vbrk
* from vbrk.
"pero para efectos de prueba haremos lo sgte:
"JANUARY
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130125'.
<vbrk>-mwsbk = 100.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130126'.
<vbrk>-mwsbk = 300.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130127'.
<vbrk>-mwsbk = 500.
"FEBRUARY
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130225'.
<vbrk>-mwsbk = 200.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130226'.
<vbrk>-mwsbk = 400.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130227'.
<vbrk>-mwsbk = 600.
"MARCH
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130325'.
<vbrk>-mwsbk = 700.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130326'.
<vbrk>-mwsbk = 800.
append initial line to lt_vbrk assigning <vbrk>.
<vbrk>-fkdat = '20130327'.
<vbrk>-mwsbk = 900.
endform. " SET_DATA
*&---------------------------------------------------------------------*
*& Form PROCESS_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form process_data .
data: lv_month type char2,
lv_mes type i.
loop at lt_vbrk assigning <vbrk>.
lv_month = <vbrk>-fkdat+4(2).
move lv_month to lv_mes.
clear <vbrk>-fkdat.
case lv_mes.
when 1.
collect <vbrk> into lt_vbrk_1.
when 2.
collect <vbrk> into lt_vbrk_2.
when 3.
collect <vbrk> into lt_vbrk_3.
endcase.
endloop.
endform. " PROCESS_DATA
EJECUTANDO & DEBUGEANDO
Tendríamos en nuestra primera consulta toda la data que incluiría todas las fechas de todos los meses.
Y aquí agrupado por mes el total correspondiente:
Total - Enero
Y aquí agrupado por mes el total correspondiente:
Total - Enero
Bienvenido las nuevas sugerencias y/o propuestas para resolver este caso!
MAGALEX
Comentarios
Publicar un comentario