开发者

Best method to supply a condition to SQL

开发者 https://www.devze.com 2023-04-12 17:16 出处:网络
I need to make a query for supply some conditions. For example:if c_Fk_CodCiiu4 is 0112.01 Or 0126.01 then ti_Fk_CodUnidadMedida should be = 01 Or 02 Or 03. In the same table, if c_Fk_CodCiiu4 is 011

I need to make a query for supply some conditions.

For example: if c_Fk_CodCiiu4 is 0112.01 Or 0126.01 then ti_Fk_CodUnidadMedida should be = 01 Or 02 Or 03. In the same table, if c_Fk_CodCiiu4 is 0113.01 Or 0113.2 then ti_Fk_CodUnidadMedida should be = 14 Or 17 Or 19. In the same table if the c_Fk_CodCiiu4 is 0114.01 Or 0114.02 Or 0114.03 then ti_Fk_CodUnidadMedida should be = 01 Or 19 Or 17.... etc.

I also have two other conditions:

  1. If c_Fk_IdBoleta = 45550711
  2. If sembrado.si_Fk_IdDesglose = desglose.si_Pk_IdDesglose

Ok, now here is the tables where is that variables.

Best method to supply a condition to SQL

And here is my query I need put all the conditions inside of query, how can I do this?

SELECT 
 'Pregunta (12).Cultivo Anual' as Numero_Pregunta,
 c_Fk_IdBoleta as Numero_Boleta,
 'La unidad de medida, no corresponde al tipo de cultivo.' as 开发者_开发知识库Detalle_Error,
 sembrado.si_Pk_NumSiembra   
  FROM
    Clt_Sembrado as sembrado, Clt_Desglose as desglose        
   WHERE ((sembrado.c_Fk_IdBoleta = 45550711)
   AND(sembrado.si_Fk_IdDesglose = desglose.si_Pk_IdDesglose) 
       AND (c_Fk_CodCiiu4 LIKE
       CASE WHEN (c_Fk_CodCiiu4 = 0112.01 
                 OR c_Fk_CodCiiu4 = 0126.01)
                 AND
                 (sembrado.ti_Fk_CodUnidadMedida <> 1
                 OR sembrado.ti_Fk_CodUnidadMedida <> 2
                 OR sembrado.ti_Fk_CodUnidadMedida <> 3
                 OR sembrado.ti_Fk_CodUnidadMedida <> 8)

                 THEN NULL))

                 ELSE 

                   ((desglose.c_Fk_CodCiiu4 = 0113.01
                    OR desglose.c_Fk_CodCiiu4 = 0113.02)
                    AND(sembrado.ti_Fk_CodUnidadMedida <> 14
                    OR sembrado.ti_Fk_CodUnidadMedida <> 17
                    OR sembrado.ti_Fk_CodUnidadMedida <> 19)))

                 ELSE 

                   ((desglose.c_Fk_CodCiiu4 = 0114.01
                    OR desglose.c_Fk_CodCiiu4 = 0114.02)
                    AND(sembrado.ti_Fk_CodUnidadMedida <> 1
                    OR sembrado.ti_Fk_CodUnidadMedida <> 19
                    OR sembrado.ti_Fk_CodUnidadMedida <> 17)))


You can take advantage of the NOT IN and IN operator.

AND(sembrado.ti_Fk_CodUnidadMedida NOT IN (1, 19, 17)
0

精彩评论

暂无评论...
验证码 换一张
取 消