Ver Mensaje Individual
  #5 (permalink)  
Antiguo 26/05/2011, 05:35
s2s1r
 
Fecha de Ingreso: febrero-2008
Mensajes: 22
Antigüedad: 16 años, 9 meses
Puntos: 0
Respuesta: Seleccionar un registro de una query con UNION

Ya he encontrado la solución y después de varias pruebas, para que me salga como queria, la select deber quedar así.

SELECT M.EAN11 AS ISBN, S.MATNR AS MATERIAL,
ISNULL((SELECT DER.MBWBEST
FROM S032 AS DER
WHERE DER.WERKS = '1003'
AND DER.LGORT = '1007' AND S.MATNR = DER.MATNR
AND DER.MBWBEST <> 0 AND DER.MBWBEST IS NOT NULL),0) AS DER,
ISNULL((SELECT BAR.MBWBEST
FROM S032 AS BAR
WHERE BAR.WERKS = '1002'
AND BAR.LGORT = '1006' AND S.MATNR = BAR.MATNR
AND BAR.MBWBEST <> 0 AND BAR.MBWBEST IS NOT NULL),0) AS BAR,
ISNULL((SELECT HUM.MBWBEST
FROM S032 AS HUM
WHERE HUM.WERKS = 'L001'
AND HUM.LGORT = '1002' AND S.MATNR = HUM.MATNR
AND HUM.MBWBEST <> 0 AND HUM.MBWBEST IS NOT NULL),0) AS HUM,
ISNULL((SELECT ECO.MBWBEST
FROM S032 AS ECO
WHERE ECO.WERKS = 'L001'
AND ECO.LGORT = '1003' AND S.MATNR = ECO.MATNR
AND ECO.MBWBEST <> 0 AND ECO.MBWBEST IS NOT NULL),0) AS ECO,
ISNULL((SELECT SSO.MBWBEST
FROM S032 AS SSO
WHERE SSO.WERKS = '1001'
AND SSO.LGORT = 'ZZZZ' AND S.MATNR = SSO.MATNR
AND SSO.MBWBEST <> 0 AND SSO.MBWBEST IS NOT NULL),0) AS SSO
FROM S032 AS S LEFT JOIN
MARA AS M ON S.MATNR = M.MATNR
WHERE S.MBWBEST <> 0 AND S.MBWBEST IS NOT NULL
GROUP BY M.EAN11, S.MATNR

de esta forma el resultado obtenido es este.

ISBN MATERIAL DER BAR HUM ECO SSO
------------------ ------------------ ----------------- ----------------- ----------------- ----------------- -----------------
9788498366099 000000000100860077 .000 3.000 .000 2.000 .000
9789586166058 000000000100675104 .000 .000 .000 2.000 42.000
9780844740195 000000000100040622 .000 .000 .000 .000 .000
9788430950355 000000000100864736 .000 1.000 .000 2.000 .000
9788436260502 000000000100878507 .000 4.000 1.000 .000 .000


De todas formas gracias, por la ayuda.