Hola buenas, creo que es aquí donde tengo que hacer esta consulta, si no es así, por favor decirmelo.
Bueno tengo una query con varias select y entre ellas tengo puesto UNION, mi pregunta es como puedo hacer para que me saque un registro por cada dato distinto y no varios como hasta ahora. Creo que no me he explicado bien, os pongo la query y el resultado actual y el que quisiera obtener. Bueno que sepais que esto lo lanzo por el analizador de consultas contra un SQL 2008.
(SELECT MARA.EAN11 AS ISBN, S032.MATNR AS MATERIAL, cast(S032.MBWBEST as int) AS DER, 0 AS BAR, 0 AS HUM, 0 AS ECO, 0 AS SSO
FROM S032 LEFT JOIN
MARA ON S032.MATNR = MARA.MATNR
WHERE S032.WERKS = '1003'
AND S032.LGORT = '1007'
AND S032.MBWBEST <> 0 AND S032.MBWBEST IS NOT NULL
AND S032.MATNR = '000000000100710046'
UNION
SELECT MARA.EAN11 AS ISBN, S032.MATNR AS MATERIAL, 0 AS DER, cast(S032.MBWBEST as int) AS BAR, 0 AS HUM, 0 AS ECO, 0 AS SSO
FROM S032 LEFT JOIN
MARA ON S032.MATNR = MARA.MATNR
WHERE S032.WERKS = '1002'
AND S032.LGORT = '1006'
AND S032.MBWBEST <> 0 AND S032.MBWBEST IS NOT NULL
AND S032.MATNR = '000000000100710046'
UNION
SELECT MARA.EAN11 AS ISBN, S032.MATNR AS MATERIAL, 0 AS DER, 0 AS BAR, cast(S032.MBWBEST as int) AS HUM, 0 AS ECO, 0 AS SSO
FROM S032 LEFT JOIN
MARA ON S032.MATNR = MARA.MATNR
WHERE S032.WERKS = 'L001'
AND S032.LGORT = '1002'
AND S032.MBWBEST <> 0 AND S032.MBWBEST IS NOT NULL
AND S032.MATNR = '000000000100710046'
UNION
SELECT MARA.EAN11 AS ISBN, S032.MATNR AS MATERIAL, 0 AS DER, 0 AS BAR, 0 AS HUM, cast(S032.MBWBEST as int) AS ECO, 0 AS SSO
FROM S032 LEFT JOIN
MARA ON S032.MATNR = MARA.MATNR
WHERE S032.WERKS = 'L001'
AND S032.LGORT = '1003'
AND S032.MBWBEST <> 0 AND S032.MBWBEST IS NOT NULL
AND S032.MATNR = '000000000100710046'
UNION
SELECT MARA.EAN11 AS ISBN, S032.MATNR AS MATERIAL, 0 AS DER, 0 AS BAR, 0 AS HUM, 0 AS ECO, cast(S032.MBWBEST as int) AS SSO
FROM S032 LEFT JOIN
MARA ON S032.MATNR = MARA.MATNR
WHERE S032.WERKS = '1001'
AND S032.LGORT = 'ZZZZ'
AND S032.MBWBEST <> 0 AND S032.MBWBEST IS NOT NULL
AND S032.MATNR = '000000000100710046'
GROUP BY MARA.EAN11, S032.MATNR
El resultado que obtengo es este
ISBN MATERIAL DER BAR HUM ECO SSO
------------------ ------------------ ----------- ----------- ----------- ----------- -----------
9780060517120 000000000100710046 0 0 0 2 0
9780060517120 000000000100710046 0 1 0 0 0
Y el que quisiera obtenere sería este:
ISBN MATERIAL DER BAR HUM ECO SSO
------------------ ------------------ ----------- ----------- ----------- ----------- -----------
9780060517120 000000000100710046 0 1 0 2 0
Espero que alguien me pueda decir si es posible, como hacerlo, muchas gracias