开发者

Query using If sentence

开发者 https://www.devze.com 2023-04-12 19:27 出处:网络
I need to make a query to validate the date of crop planting, and crop harvest. From the table \"Clt_Sembrado\", depending of the code of cultivation in table Clt_Desglose

I need to make a query to validate the date of crop planting, and crop harvest. From the table "Clt_Sembrado", depending of the code of cultivation in table Clt_Desglose

For example:

If the product(cultivation) have the code (table "Clt_Desglose") = 0111.01 OR 0111.02, then the date of harvest (d_MesAnioSiembra in Clt_Sembrado) must be less开发者_JAVA百科 than the date of planting (d_MesAnioCosecha in Clt_Sembrado) + 6 months

I'm trying to build the query. But I don't know how make the If sentence.

Here is the query, but I need help for validate this.

    SELECT 
 c_Fk_IdBoleta as Numero_Boleta,
 'Pregunta (12)' as Numero_Pregunta,
 'El período de siembra y cosecha no corresponde al tipo de cultivo' as Detalle_Error
  FROM
    Clt_Sembrado as sembrado
  WHERE
     sembrado.c_Fk_IdBoleta = 45550711
    GROUP BY sembrado.c_Fk_IdBoleta, sembrado.d_MesAnioSiembra, sembrado.d_MesAnioCosecha, sembrado.si_Fk_IdDesglose
    HAVING sembrado.d_MesAnioCosecha < (SELECT c_Fk_CodCiiu4
                                        FROM Clt_Desglose as desglose
                                        WHERE sembrado.si_Fk_IdDesglose = desglose.si_Pk_IdDesglose)

And here is the tables where I need to take the values:

Query using If sentence

How can I make this query?? thanks..


You implement conditionals by using the CASE statement.

Example:

select 
   name,
   age,
   case 
       when age < 13 then 'Child'
       when age between 13 and 17 'Teenager'
       when age between 18 and 20 'Young Adult'
       else 'Adult'
   end as AgeGroup,
   case gender
       when 'M' then 'Male'
       when 'F' then 'Female'
       else 'Unknown'
   end as Gender
from YourTable

Adapt the concept to your query accordingly.

0

精彩评论

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

关注公众号