开发者

Query for compare several dates

开发者 https://www.devze.com 2023-04-12 03:13 出处:网络
I need compare some dates, my query is getting 3 dates in first query => 2011-09-01, 2011-10-01, 2011-11-01

I need compare some dates, my query is getting 3 dates in first query => 2011-09-01, 2011-10-01, 2011-11-01

And 3 dates in second query => 2011-07-01, 2011-11-01, 2011-08-01

So How can I compare them and avoid this sql error?:

"Subquery returned more than one value, which is not correct when it is following =,! =, <, <=,>,> = Or when used as an expression."

Thanks

SELECT 
 c_Fk_IdBoleta as Numero_Boleta,
 'Pregunta (12)' as Numero_Pregunta,
 'El período transcurrido entre siembra y cosecha no 开发者_Python百科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 
   ( SELECT  sembrado.d_MesAnioCosecha 
   FROM Clt_Sembrado as sembrado, Clt_Desglose as desglose 
   WHERE sembrado.si_Fk_IdDesglose = desglose.si_Pk_IdDesglose 
   AND sembrado.c_Fk_IdBoleta = 45550711
   )                                                           >           (SELECT DATEADD(Month,6,sembrado.d_MesAnioSiembra)
                                                                            FROM Clt_Desglose as desglose, Clt_Sembrado as sembrado
                                                                            WHERE (((sembrado.si_Fk_IdDesglose = desglose.si_Pk_IdDesglose)AND
                                                                            (desglose.c_Fk_CodCiiu4 = 0112.00
                                                                            OR desglose.c_Fk_CodCiiu4 = 0111.01
                                                                            OR desglose.c_Fk_CodCiiu4 = 0111.02
                                                                            OR desglose.c_Fk_CodCiiu4 = 0113.01
                                                                            OR desglose.c_Fk_CodCiiu4 = 0113.03
                                                                            OR desglose.c_Fk_CodCiiu4 = 0113.05
                                                                            OR desglose.c_Fk_CodCiiu4 = 0113.06
                                                                            OR desglose.c_Fk_CodCiiu4 = 0113.07
                                                                            OR desglose.c_Fk_CodCiiu4 = 0113.08
                                                                            OR desglose.c_Fk_CodCiiu4 = 0113.10                            
                                                                            ))
                                                                            AND sembrado.c_Fk_IdBoleta = 45550711))

Here is the tables: The problem is one cultive could be inserted ONE or MORE times in the table Blt_Sembrado, and I need validate that the d_MesAnioCosecha is less (d_MesAnioSiembra + 6 months). FOR EACH CULTIVE INSERTED in the table Blt_Sembrado.

Query for compare several dates


Your problem is here:

AND sembrado.c_fk_idboleta = 40480711) > (SELECT ...

In order to make this work you have to ensure your sub-select will return at most one value.

If your date must be greater than all dates returned, then it's the same as being greater than the maximum date returned, thus using MAX will solve your problem:

AND sembrado.c_fk_idboleta = 40480711) > (SELECT 
   Dateadd(MONTH, 6, max(sembrado.d_mesaniosiembra))
...
0

精彩评论

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

关注公众号