PDA

Ver la Versión Completa : Como harian este query, me da error


javico40
12/11/11, 19:56:09
SELECT mseg~matnr
makt~maktx
mseg~menge
mseg~dmbtr
mkpf~budat
FROM mseg
INNER JOIN (SELECT mseg~matnr
SUM( mseg~menge )
SUM( mseg~dmbtr )
FROM mseg
INTO CORRESPONDING FIELDS OF TABLE pre_mattab_recieve
WHERE mseg~bwart IN ('101', '102', '501', '502')
GROUP BY mseg~matnr) AS b
ON mseg~matnr EQ b~matnr
INNER JOIN makt
ON mseg~matnr EQ makt~matnr
AND makt~spras EQ 'ES'
INNER JOIN mkpf
ON mseg~mblnr EQ mkpf~mblnr
AND mseg~mjahr EQ mkpf~mjahr
INNER JOIN lfa1
ON mseg~lifnr EQ lfa1~lifnr
INTO CORRESPONDING FIELDS OF TABLE pre_outtab
WHERE mseg~bwart IN ('561', '562')
AND mseg~bwart IN Type " Sel. Screen
AND mseg~matnr IN Material " Sel. Screen
AND mkpf~budat EQ Posting. " Sel. Screen

jtristan
14/11/11, 08:00:03
Entiendo que quieres sacar el importe detallado por fecha de contabilidad para los registros en la tabla pre_outtab al filtrar por proveedor.
¿Porqué no haces la primera select en un primer paso, lo almacenas en la tabla interna y luego la unes con la segunda, con un for all entries in ?

Un saludo.

javico40
14/11/11, 13:50:09
jtristan puedo unir las tablas internas a tablas en la base de datos, no sabia que se podia, voy a probar lo que me dices, lo que estaba haciendo era consultar la informacion individualmente y llenar tres tablas internas, luego no supe como unirlas, voy a probar con for all entries.

jtristan
14/11/11, 14:05:33
Lo único, sino recuerdo mal, tienes que tener con el caso de que tengas registros idénticos, pues el for all entries se comporta como si aplicase un distinct.
Es decir, si tienes:

material1 60 euros
material2 25 euros
material1 60 euros

cuando enlaces con la tabla, por ejemplo de descripciones de materiales sólo te saldrían dos registros

material1 60 euros desc. material1
material2 25 euros desc. material2

Para estos casos, la solución sería, poner un campo que siempre nos diferencia los registros, por ejemplo, la posición en las líneas de los pedidos, o hacer un loop at de la tabla y un select para cada uno de los registros de la tabla. Esta última opción tiene una carga mayor del rendimiento.

Un saludo.

javico40
14/11/11, 14:35:13
Lo que me pidieron exactamente es consultar la misma tabla, pero con diferentes movimientos bwart, se me hizo complicado por eso, me pidieron algo asi

codigoMaterial nombreMaterial Cantidad(bwart 561 562) Monto(bwart 561 562) Cantidad(bwart 501 502 101 102) Monto(bwart 501 502 101 102) Cantidad(bwart 201 202 281 282) Monto(bwart 201 202 281 282)

Basicamente es la misma tabla MSEG pero diferentes movimientos bwart, asi que no pude usar inner join y es complico todo, tengo 1 semana buscando una solucion, muchas gracias por tu ayuda jtristan por fin veo algo de luz.

como es un reporte ALV defini una tabla interna para llenar el ALV (preoutab) asi que ahora me toca ver como meto todos los select en esa tabla. muchas gracias, Un saludo.

javico40
14/11/11, 14:36:08
Los duplicados los elimino asi

DELETE ADJACENT DUPLICATES

segun estoy leyendo en los ejemplos

jtristan
15/11/11, 06:59:52
Si no te entiendo mal, lo que necesitas es una tabla en que tengas:

material descripción cantidad importe.

Sólo que vas a tener que hacerlo para varias opciones de movimientos. Si las condiciones de los movimientos sabes cuáles son, es decir, que te dicen que tienes que mostrarlo siempre para el 561 y 562 por una lado y para el 501 502 101 102 por otro puedes hacer algo así.

data: r_movimientos type range of mseg-bwart,
wa_movimiento like line of r_movimientos,
indice type i.
types: begin of movimiento_type,
matnr type mseg-matnr,
maktx type makt-maktx,
menge type mseg-menge,
dmbtr type mseg-dmbtr,
end of movimiento_type,
t_movimientos_type type table of movimiento_type.

data: t_movimientos type t_movimientos_type with HEADER LINE,
t_movimientos1 type t_movimientos_type with HEADER LINE,
t_movimientos2 type t_movimientos_type with HEADER LINE.

do 2 times.
indice = sy-index.

if indice = 1.
wa_movimiento-sign = 'I'.
wa_movimiento-option = 'EQ'.
wa_movimiento-low = '561'.
append wa_movimiento to r_movimientos.

wa_movimiento-low = '562'.
append wa_movimiento to r_movimientos.
elseif sy-index = 2.
refresh r_movimientos.
wa_movimiento-sign = 'I'.
wa_movimiento-option = 'EQ'.
wa_movimiento-low = '501'.
append wa_movimiento to r_movimientos.

wa_movimiento-low = '502'.
append wa_movimiento to r_movimientos.

wa_movimiento-low = '101'.
append wa_movimiento to r_movimientos.

wa_movimiento-low = '102'.
append wa_movimiento to r_movimientos.

endif.

SELECT mseg~matnr
makt~maktx
sum( mseg~menge )
sum( mseg~dmbtr )
FROM
mkpf
inner join
mseg
on
mkpf~mblnr = mseg~mblnr and
mkpf~mjahr = mseg~mjahr
inner join
makt
on
mseg~matnr = makt~matnr and
makt~spras = sy-langu
INTO CORRESPONDING FIELDS OF TABLE t_movimientos
WHERE mseg~bwart IN r_movimientos and
budat between '20100101' and '20100131'
GROUP BY mseg~matnr
makt~maktx.

if indice = 1.
t_movimientos1[] = t_movimientos[].
elseif indice = 2.
t_movimientos2[] = t_movimientos[].
endif.
enddo.

Es otra forma. Si fuesen muchos rangos de movimientos el if sería una chapuza y se podría buscar otra forma, pero para dos puede funcionar.

Un saludo.

javico40
15/11/11, 12:32:28
Hola jtristan lo que me piden es aso:

CodigoMaterial Material Cantidad Monto (561,562) Cantidad Monto (201,202,261,202,281,282) Cantidad Monto(101,105,501,502)

Es mostrar las columnas de cantidad y monto para diferentes movimientos pero separadas, hoy me dijeron que tengo que hacerles reverse, 561-562 por ejemplo, para sacar una cantidad aun no me he puesto a pensar en como hacer eso, jajajaja, con lo que investigue de internet hice lo siguiente lo cual funciona mas o menos bien.

TYPES: BEGIN OF t_materials_description,
matnr TYPE mseg-matnr,
maktx TYPE makt-maktx,
budat TYPE mkpf-budat,
END OF t_materials_description,

BEGIN OF t_initial,
matnr TYPE mseg-matnr,
menge TYPE mseg-menge,
dmbtr TYPE mseg-dmbtr,
END OF t_initial,

BEGIN OF t_issue,
matnr TYPE mseg-matnr,
menge TYPE mseg-menge,
dmbtr TYPE mseg-dmbtr,
END OF t_issue,

BEGIN OF t_receip,
matnr TYPE mseg-matnr,
menge TYPE mseg-menge,
dmbtr TYPE mseg-dmbtr,
END OF t_receip.

DATA: wa_matdescription TYPE t_materials_description,
it_matdescription TYPE TABLE OF t_materials_description,
it_matdescription_aux TYPE TABLE OF t_materials_description,
wa_matinitial TYPE t_initial,
it_matinitial TYPE TABLE OF t_initial,
wa_matissue TYPE t_issue,
it_matissue TYPE TABLE OF t_issue,
wa_matreceip TYPE t_receip,
it_matreceip TYPE TABLE OF t_receip,
cantidad TYPE sy-dbcnt.

* OBTENGO LA LISTA DE MATERIALES DESDE MSEG
SELECT mseg~matnr
makt~maktx
FROM mseg
INNER JOIN makt
ON mseg~matnr EQ makt~matnr
AND makt~spras EQ 'ES'
INNER JOIN mkpf
ON mseg~mblnr EQ mkpf~mblnr
AND mseg~mjahr EQ mkpf~mjahr
INNER JOIN lfa1
ON mseg~lifnr EQ lfa1~lifnr
INTO TABLE it_matdescription
WHERE mkpf~budat IN Material " Sel. Screen
AND mseg~lifnr IN Type " Sel. Screen
AND mseg~gjahr EQ Posting. " Sel. Screen


IF sy-subrc EQ 0.
* SI EXISTEN MATERIALES
* CUENTO CUANTOS EGISTROS OBTUVE
cantidad = sy-dbcnt.
* COPIO MI TABLA A UNA TABLA AUXILIAR
it_matdescription_aux[] = it_matdescription[].
* ORDENO LA TABLA
SORT it_matdescription_aux BY matnr.

DELETE ADJACENT DUPLICATES FROM it_matdescription_aux COMPARING matnr.

SELECT mseg~matnr
mseg~menge
mseg~dmbtr
FROM MSEG INTO TABLE it_matinitial
FOR ALL ENTRIES IN it_matdescription_aux
WHERE mseg~bwart IN ('561', '562')
AND matnr = it_matdescription_aux-matnr.

IF sy-subrc EQ 0.

it_matdescription_aux[] = it_matdescription[].

SORT it_matdescription_aux BY matnr.

DELETE ADJACENT DUPLICATES FROM it_matdescription_aux COMPARING matnr.

SELECT mseg~matnr
mseg~menge
mseg~dmbtr
FROM MSEG INTO TABLE it_matissue
FOR ALL ENTRIES IN it_matdescription_aux
WHERE mseg~bwart IN ('281', '282', '261', '262', '201', '202')
AND matnr = it_matdescription_aux-matnr.

IF sy-subrc EQ 0.

it_matdescription_aux[] = it_matdescription[].

SORT it_matdescription_aux BY matnr.

DELETE ADJACENT DUPLICATES FROM it_matdescription_aux COMPARING matnr.

SELECT mseg~matnr
mseg~menge
mseg~dmbtr
FROM MSEG INTO TABLE it_matreceip
FOR ALL ENTRIES IN it_matdescription_aux
WHERE mseg~bwart IN ('101', '102', '501', '502')
AND matnr = it_matdescription_aux-matnr.

SORT it_matinitial BY matnr.
SORT it_matissue BY matnr.
SORT it_matreceip BY matnr.

LOOP AT it_matdescription INTO wa_matdescription.

READ TABLE it_matinitial INTO wa_matinitial
WITH KEY matnr = wa_matdescription-matnr
BINARY SEARCH.

IF sy-subrc EQ 0.

READ TABLE it_matissue INTO wa_matissue
WITH KEY matnr = wa_matdescription-matnr
BINARY SEARCH.

IF sy-subrc EQ 0.

READ TABLE it_matreceip INTO wa_matreceip
WITH KEY matnr = wa_matdescription-matnr
BINARY SEARCH.

IF sy-subrc EQ 0.

wa_outtab-matnr = wa_matdescription-matnr.
wa_outtab-maktx = wa_matdescription-maktx.
wa_outtab-menge = wa_matinitial-menge.
wa_outtab-dmbtr = wa_matinitial-dmbtr.
wa_outtab-menge2 = wa_matissue-menge.
wa_outtab-dmbtr2 = wa_matissue-dmbtr.
wa_outtab-menge3 = wa_matreceip-menge.
wa_outtab-dmbtr3 = wa_matreceip-dmbtr.
INSERT wa_outtab INTO TABLE pre_outtab.

ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.
ENDIF.

jtristan
15/11/11, 13:35:28
Si te surge cualquier problema coméntalo a ver si te podemos ayudar.

Un saludo.

javico40
15/11/11, 13:50:14
Muchas gracias jtristan, trato de no molestar mucho yo tambien ayudo en foros de programacion en ph y java que es lo que mas se, sap tengo usandolo 4 dias, y siento que voy avanzando muy bien.

DCErick
15/11/11, 14:57:09
WHERE mkpf~budat IN Material " Sel. Screen
AND mseg~lifnr IN Type " Sel. Screen
AND mseg~gjahr EQ Posting. " Sel. Screen



Oye ese Selection-Screen "Material" es de tipo Fecha o es el Sel. Screen donde colocas los materiales que quieres filtrar? Lo que pasa es que lo estas comparando contra una variable de tipo fecha.

javico40
15/11/11, 15:10:19
DCErick Esa seccion de codigo es vieja, la reemplaze con lo siguiente:


SELECT-OPTIONS: Material FOR mseg-matnr,
Type FOR marav-mtart,
Posting FOR mkpf-budat.
SELECTION-SCREEN END OF BLOCK b1.


Asi saco el codigo de material de mseg aunque pensandolo bien deberia salir de makt, :) el tipo de material de marav, y la fecha de la entrada de mkpf.

Ya los mas dificil esta hecho, lo que no he podido hacer es la sumatoria que me dijo jtristan, me sale un the addition FOR ALL ENTRIES excludes all aggregate functions with the exception of COUNT(*).


SELECT mseg~matnr
mseg~menge
mseg~dmbtr
FROM MSEG INTO TABLE it_matissue
FOR ALL ENTRIES IN it_matdescription_aux
WHERE mseg~bwart IN ('201', '202', '261', '262', '281', '282')
AND matnr = it_matdescription_aux-matnr.


Otra cosa tambien es que en mseg el valor menge todo en positivo muestra valores negativos como positivos, donde deberia ir -40 dice 40 en menge, por alli lei que los negativos se sacan uniendo otra tabla, wow, esto del open sql deja muchos limitantes. es mi opinion.

DCErick
15/11/11, 15:13:49
Creo que lo que tu necesitas es ver es la transacción MB51 este es el programa que usa RM07DOCS el form que podria interesarte es el data_selection.

Lo que te piden creo que no es tan dificil. Yo lo que haria es adaptar el select que está en ese programa en el form data_selection, lo que le adaptaría es lo que está en el Where dejando solo los campos que necesito evaluar y tambien los campos que requiero que se regresen, creo que tu ocupas 3 campos.

Bueno a esos 3, le adicionas el campo MSEG~SHKZG para que tengas el 'Indicador debe/haber' el cual es el que controla el signo del movimiento y poder haces tus restas/sumas, este campo toma los valores de S y H (S - Cargo, H - Abono) (S - Positivo, H - Negativo) y tambie le adiciones el campo BWART que es donde está el tipo de movimiento.

Bueno ya que tengas tu tabla interna con todo el churrero de movimientos le haces un loop at y dependiendo del campo BWART y SHKZG le sumas o restas al campo correspondiente de tu tabla interna final.

Bueno creo que con 2 tablas es suficiente, una para obtener los movimientos y otra para contener el resultado final xD.

DCErick
15/11/11, 15:17:29
Otra cosa tambien es que en mseg el valor menge todo en positivo muestra valores negativos como positivos, donde deberia ir -40 dice 40 en menge, por alli lei que los negativos se sacan uniendo otra tabla, wow, esto del open sql deja muchos limitantes. es mi opinion.

Mandaste esto antes de que terminara de escribir mi post, igual hay tienes la respuesta xD.

Cualquier duda aqui andamos.