0

Tengo esta tabla que se llama SI_Consecutivo introducir la descripción de la imagen aquí

lo que estoy tratado de realizar es que si SI_Estado = 0 y SI_Num_Conteo = 2

Me haga esta consulta

SELECT 
SI_Articulo = COALESCE(t.SI_Articulo, c.SI_Num_Articulo), 
SI_Ubicacion = COALESCE(t.SI_Ubicacion, c.SI_Ubicacion), 
SI_OV = COALESCE(c.SI_OV,''), 
SI_Ubicacion_Fisica = COALESCE(t.SI_Ubicacion_Fisica,c.SI_Ubicacion_Fisica), 
SI_Existencia = COALESCE(t.SI_Existencia, 0), 
SI_Cantidad = COALESCE(c.SI_Cantidad, 0), 
/*SI_Cantidad2 = (SELECT COALESCE(c2.SI_Cantidad,0)  
FROM SI_Conteo c2 
WHERE  c2.SI_Num_Conteo = 2 AND t.SI_Ubicacion = c2.SI_Ubicacion), */
SI_Dif =  COALESCE(c.SI_Cantidad, 0) - COALESCE(t.SI_Existencia, 0), 
SI_Dif_Dinero = (COALESCE(c.SI_Cantidad,0) - COALESCE(t.SI_Existencia,0)) * COALESCE(m.SI_Costo_Promedio,0)
FROM SI_Inventario_Teorico_QAD t 
full JOIN SI_Conteo c 
ON t.SI_Articulo = c.SI_Num_Articulo  
AND t.SI_Ubicacion = c.SI_Ubicacion  
INNER JOIN SI_Maestro_Ref_QAD m  
ON  t.SI_Articulo = m.SI_Num_Articulo 
OR c.SI_Num_Articulo = m.SI_Num_Articulo 
WHERE 
(c.SI_Num_Articulo = 201533 OR t.SI_Articulo = 201533)
AND c.SI_Num_Conteo = 2)

y si no me haga esta otra

SELECT 
SI_Articulo = COALESCE(t.SI_Articulo, c.SI_Num_Articulo), 
SI_Ubicacion = COALESCE(t.SI_Ubicacion, c.SI_Ubicacion), 
SI_OV = COALESCE(c.SI_OV,''), 
SI_Ubicacion_Fisica = COALESCE(t.SI_Ubicacion_Fisica,c.SI_Ubicacion_Fisica), 
SI_Existencia = COALESCE(t.SI_Existencia, 0), 
SI_Cantidad = COALESCE(c.SI_Cantidad, 0), 
/*SI_Cantidad2 = (SELECT COALESCE(c2.SI_Cantidad,0)  
FROM SI_Conteo c2 
WHERE  c2.SI_Num_Conteo = 2 AND t.SI_Ubicacion = c2.SI_Ubicacion), */
SI_Dif =  COALESCE(c.SI_Cantidad, 0) - COALESCE(t.SI_Existencia, 0), 
SI_Dif_Dinero = (COALESCE(c.SI_Cantidad,0) - COALESCE(t.SI_Existencia,0)) * COALESCE(m.SI_Costo_Promedio,0)
FROM SI_Inventario_Teorico_QAD t 
full JOIN SI_Conteo c 
ON t.SI_Articulo = c.SI_Num_Articulo  
AND t.SI_Ubicacion = c.SI_Ubicacion  
INNER JOIN SI_Maestro_Ref_QAD m  
ON  t.SI_Articulo = m.SI_Num_Articulo 
OR c.SI_Num_Articulo = m.SI_Num_Articulo 
WHERE 
(c.SI_Num_Articulo = 201533 OR t.SI_Articulo = 201533)
AND c.SI_Num_Conteo = 1

Yo lo estaba realizando de esta manera pero no me funciona. Me da el siguiente error

Sólo se puede especificar una expresión en la lista de selección cuando la subconsulta no se especifica con EXISTS.

SELECT SI_Estado, SI_Num_Conteo,
(CASE WHEN SI_Estado = 0 and SI_Num_Conteo = 2 THEN 
(SELECT 
SI_Articulo = COALESCE(t.SI_Articulo, c.SI_Num_Articulo), 
SI_Ubicacion = COALESCE(t.SI_Ubicacion, c.SI_Ubicacion), 
SI_OV = COALESCE(c.SI_OV,''), 
SI_Ubicacion_Fisica = COALESCE(t.SI_Ubicacion_Fisica,c.SI_Ubicacion_Fisica), 
SI_Existencia = COALESCE(t.SI_Existencia, 0), 
SI_Cantidad = COALESCE(c.SI_Cantidad, 0), 
/*SI_Cantidad2 = (SELECT COALESCE(c2.SI_Cantidad,0)  
FROM SI_Conteo c2 
WHERE  c2.SI_Num_Conteo = 2 AND t.SI_Ubicacion = c2.SI_Ubicacion), */
SI_Dif =  COALESCE(c.SI_Cantidad, 0) - COALESCE(t.SI_Existencia, 0), 
SI_Dif_Dinero = (COALESCE(c.SI_Cantidad,0) - COALESCE(t.SI_Existencia,0)) * COALESCE(m.SI_Costo_Promedio,0)
FROM SI_Inventario_Teorico_QAD t 
full JOIN SI_Conteo c 
ON t.SI_Articulo = c.SI_Num_Articulo  
AND t.SI_Ubicacion = c.SI_Ubicacion  
INNER JOIN SI_Maestro_Ref_QAD m  
ON  t.SI_Articulo = m.SI_Num_Articulo 
OR c.SI_Num_Articulo = m.SI_Num_Articulo 
WHERE 
(c.SI_Num_Articulo = 201533 OR t.SI_Articulo = 201533)
AND c.SI_Num_Conteo = 2)
ELSE
(SELECT 
SI_Articulo = COALESCE(t.SI_Articulo, c.SI_Num_Articulo), 
SI_Ubicacion = COALESCE(t.SI_Ubicacion, c.SI_Ubicacion), 
SI_OV = COALESCE(c.SI_OV,''), 
SI_Ubicacion_Fisica = COALESCE(t.SI_Ubicacion_Fisica,c.SI_Ubicacion_Fisica), 
SI_Existencia = COALESCE(t.SI_Existencia, 0), 
SI_Cantidad = COALESCE(c.SI_Cantidad, 0), 
/*SI_Cantidad2 = (SELECT COALESCE(c2.SI_Cantidad,0)  
FROM SI_Conteo c2 
WHERE  c2.SI_Num_Conteo = 2 AND t.SI_Ubicacion = c2.SI_Ubicacion), */
SI_Dif =  COALESCE(c.SI_Cantidad, 0) - COALESCE(t.SI_Existencia, 0), 
SI_Dif_Dinero = (COALESCE(c.SI_Cantidad,0) - COALESCE(t.SI_Existencia,0)) * COALESCE(m.SI_Costo_Promedio,0)
FROM SI_Inventario_Teorico_QAD t 
full JOIN SI_Conteo c 
ON t.SI_Articulo = c.SI_Num_Articulo  
AND t.SI_Ubicacion = c.SI_Ubicacion  
INNER JOIN SI_Maestro_Ref_QAD m  
ON  t.SI_Articulo = m.SI_Num_Articulo 
OR c.SI_Num_Articulo = m.SI_Num_Articulo 
WHERE 
(c.SI_Num_Articulo = 201533 OR t.SI_Articulo = 201533)
AND c.SI_Num_Conteo = 1)
END)
FROM SI_Consecutivo
3
  • Consejo amistoso: por favor no pongas el título de tus preguntas en mayúscula y ofrece un poco mas de información en el mismo. Respecto a tu pregunta: ¿hay alguna posibilidad de cambiar la forma en que haces tu consulta para dividir la lógica? el 28 jul. 2017 a las 17:52
  • Que tal Eduard, por lo que veo sigues con esas tablas, que necesitas mostrar? parece que tu pregunta apunta a que por las condiciones de los joins, el resultado es diferente vdd? otra cosa SI_Estado = 0, por lo que veo es necesario en el where.
    – Rostan
    el 28 jul. 2017 a las 18:30
  • Si, puede cambiar la forma de la consulta
    – Eduard
    el 28 jul. 2017 a las 18:39

2 respuestas 2

1

Amigo, el case when se usa para devolver un dato como tal no para ejecutar alguna secuencia por eso debes de usar if else

quedaria algo asi:

SELECT SI_Estado, SI_Num_Conteo,
    (IF SI_Estado = 0 and SI_Num_Conteo = 2
    begin
        SELECT 
            SI_Articulo = COALESCE(t.SI_Articulo, c.SI_Num_Articulo), 
            SI_Ubicacion = COALESCE(t.SI_Ubicacion, c.SI_Ubicacion), 
            SI_OV = COALESCE(c.SI_OV,''), 
            SI_Ubicacion_Fisica = COALESCE(t.SI_Ubicacion_Fisica,c.SI_Ubicacion_Fisica), 
            SI_Existencia = COALESCE(t.SI_Existencia, 0), 
            SI_Cantidad = COALESCE(c.SI_Cantidad, 0), 
            /*SI_Cantidad2 = (SELECT COALESCE(c2.SI_Cantidad,0)  
                FROM SI_Conteo c2 
                WHERE  c2.SI_Num_Conteo = 2 AND t.SI_Ubicacion = c2.SI_Ubicacion), */
            SI_Dif =  COALESCE(c.SI_Cantidad, 0) - COALESCE(t.SI_Existencia, 0), 
            SI_Dif_Dinero = (COALESCE(c.SI_Cantidad,0) - COALESCE(t.SI_Existencia,0)) * COALESCE(m.SI_Costo_Promedio,0)
        FROM SI_Inventario_Teorico_QAD t 
            full JOIN SI_Conteo c ON t.SI_Articulo = c.SI_Num_Articulo  
                AND t.SI_Ubicacion = c.SI_Ubicacion  
            INNER JOIN SI_Maestro_Ref_QAD m  ON  t.SI_Articulo = m.SI_Num_Articulo 
                OR c.SI_Num_Articulo = m.SI_Num_Articulo 
        WHERE 
            (c.SI_Num_Articulo = 201533 OR t.SI_Articulo = 201533)
                AND c.SI_Num_Conteo = 2)
    end
ELSE
    begin
        SELECT 
            SI_Articulo = COALESCE(t.SI_Articulo, c.SI_Num_Articulo), 
            SI_Ubicacion = COALESCE(t.SI_Ubicacion, c.SI_Ubicacion), 
            SI_OV = COALESCE(c.SI_OV,''), 
            SI_Ubicacion_Fisica = COALESCE(t.SI_Ubicacion_Fisica,c.SI_Ubicacion_Fisica), 
            SI_Existencia = COALESCE(t.SI_Existencia, 0), 
            SI_Cantidad = COALESCE(c.SI_Cantidad, 0), 
            /*SI_Cantidad2 = (SELECT COALESCE(c2.SI_Cantidad,0)  
            FROM SI_Conteo c2 
            WHERE  c2.SI_Num_Conteo = 2 AND t.SI_Ubicacion = c2.SI_Ubicacion), */
            SI_Dif =  COALESCE(c.SI_Cantidad, 0) - COALESCE(t.SI_Existencia, 0), 
            SI_Dif_Dinero = (COALESCE(c.SI_Cantidad,0) - COALESCE(t.SI_Existencia,0)) * COALESCE(m.SI_Costo_Promedio,0)
        FROM SI_Inventario_Teorico_QAD t 
            full JOIN SI_Conteo c ON t.SI_Articulo = c.SI_Num_Articulo  
                AND t.SI_Ubicacion = c.SI_Ubicacion  
            INNER JOIN SI_Maestro_Ref_QAD m  ON  t.SI_Articulo = m.SI_Num_Articulo 
                OR c.SI_Num_Articulo = m.SI_Num_Articulo 
        WHERE 
            (c.SI_Num_Articulo = 201533 OR t.SI_Articulo = 201533)
                AND c.SI_Num_Conteo = 1
    end
)
FROM SI_Consecutivo
0

Veo que ambos querys solo cambian en la ultima condición, entonces el case lo puedes manejar así:

SELECT SI_Articulo = COALESCE(t.SI_Articulo, c.SI_Num_Articulo), 
       SI_Ubicacion = COALESCE(t.SI_Ubicacion, c.SI_Ubicacion), 
       SI_OV = COALESCE(c.SI_OV,''), 
       SI_Ubicacion_Fisica = COALESCE(t.SI_Ubicacion_Fisica,c.SI_Ubicacion_Fisica), 
SI_Existencia = COALESCE(t.SI_Existencia, 0), 
SI_Cantidad = COALESCE(c.SI_Cantidad, 0), 
SI_Dif =  COALESCE(c.SI_Cantidad, 0) - COALESCE(t.SI_Existencia, 0), 
SI_Dif_Dinero = (COALESCE(c.SI_Cantidad,0) - COALESCE(t.SI_Existencia,0)) * COALESCE(m.SI_Costo_Promedio,0)                          
FROM SI_Inventario_Teorico_QAD t                   
full JOIN SI_Conteo c 
ON t.SI_Articulo = c.SI_Num_Articulo  
AND t.SI_Ubicacion = c.SI_Ubicacion  
INNER JOIN SI_Maestro_Ref_QAD m  
ON  t.SI_Articulo = m.SI_Num_Articulo 
OR c.SI_Num_Articulo = m.SI_Num_Articulo 
where (C.SI_NUM_ARTICULO = 201533 or T.SI_ARTICULO = 201533)   
and SI_NUM_CONTEO = (case SI_ESTADO when '0' then  2 else 1 end)
1
  • Este AND and SI_NUM_CONTEO = (case SI_ESTADO when '0' then 2 else 1 end) no esta haciendo nada. igual me los muestra todos.
    – Eduard
    el 28 jul. 2017 a las 19:16

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.