CASE WHEN FOR A SELECT

0

I have this table that is called SI_Consecutivo

What I am trying to do is that if SI_Estado = 0 and SI_Num_Conteo = 2

Ask me this question

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)

and if not make me this other one

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

I was doing it this way but it does not work for me. I get the following error

  

Only one expression can be specified in the selection list   when the subquery is not specified with 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
    
asked by Eduard 28.07.2017 в 19:03
source

2 answers

1

Friend, the case when is used to return a data as such not to execute any sequence so you must use if else

would be something like this:

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
    
answered by 28.07.2017 / 20:29
source
0

I see that both querys only change in the last condition, then You can handle the case like this:

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)
    
answered by 28.07.2017 в 20:06