2015-01-16 2 views
0

다음 SQL이 주어지면 문제가 발생합니다. 시작일 (@fi)과 종료일 (@ff)에 따라 경우에 따라 값을 반환하는 경우가 있습니다.범위의 날짜 문제

: 1) @fi IF 및 @ff하고/01/2014 '01 -> 미리 확인

  2)if @fi and @ff are '01/01/2015' -> ok 
     3)if @fi and @ff are '31/12/2014' -> no rows 
     4)if @fi = '31/12/2014' and @ff = '01/01/2015' -> ok (but returns more rows than the second case, that is, there should be results for the third case). 
I've tried with BETWEEN clause, with the same result. I checked (in the 3rd case) if the is included in that range (with an if-else), and yes, it is. Any clue? 

감사. 감사합니다.

declare @fi date, 
    @ff date 

    set @fi = '31/12/2014' 
    set @ff = '01/01/2015' 
SELECT Registro, Area, Grupo, Codigo, Asunto, Año, Total, IdArea, IdGrupo 
    FROM 
     (SELECT TOP (100) PERCENT TM2000_RegistrosAsuntos.Registro, 
       TM2000_Areas.Area, TM2000_Grupos.Grupo, 
       TM2000_Asuntos.Codigo, TM2000_Asuntos.Asunto, 
       SUM(CAST(CASE WHEN FechaDocumento >= @FI 
        AND FechaDocumento <= @FF THEN 1 ELSE 0 END 
        AS integer)) 
        AS Año, COUNT(TM2000_RegistroGeneral.Id) AS Total, 
        TM2000_Asuntos.IdArea, TM2000_Asuntos.IdGrupo 
      FROM TM2000_RegistroGeneral 
      INNER JOIN TM2000_Asuntos ON 
       TM2000_RegistroGeneral.IdAsunto = TM2000_Asuntos.Codigo 
      INNER JOIN 
       TM2000_Areas ON TM2000_Asuntos.IdArea = TM2000_Areas.Id 
      INNER JOIN 
       TM2000_Grupos ON TM2000_Asuntos.IdGrupo = TM2000_Grupos.Id AND TM2000_Asuntos.IdArea = TM2000_Grupos.IdArea 
      INNER JOIN 
       TM2000_RegistrosAsuntos ON TM2000_RegistroGeneral.TipoRegistro = TM2000_RegistrosAsuntos.Id 
      WHERE (TM2000_RegistroGeneral.FechaDocumento >= @FI 
            AND TM2000_RegistroGeneral.FechaDocumento <= @FF) 
            AND (ISNULL(TM2000_Asuntos.CodigoNulo, 0) = 0) 

            OR (TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, - 1, @FI) 
            AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, - 1, @FF)) 
            AND (ISNULL(TM2000_Asuntos.CodigoNulo, 0) = 0) 

            OR (TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, - 2, @FI) 
            AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, - 2, @FF)) 
            AND (ISNULL(TM2000_Asuntos.CodigoNulo, 0) = 0) 

            OR (TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, - 3, @FI) 
            AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, - 3, @FF)) 
            AND (ISNULL(TM2000_Asuntos.CodigoNulo, 0) = 0) 

            OR (TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, - 4, @FI) 
            AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, - 4, @FF)) 
            AND (ISNULL(TM2000_Asuntos.CodigoNulo, 0) = 0) 
          GROUP BY TM2000_Asuntos.Asunto, TM2000_Asuntos.IdArea, TM2000_Asuntos.IdGrupo, TM2000_Asuntos.Codigo, TM2000_Areas.Area, TM2000_Grupos.Grupo, 
                TM2000_RegistrosAsuntos.Registro 
          ORDER BY TM2000_RegistrosAsuntos.Registro, TM2000_Asuntos.IdArea, TM2000_Asuntos.IdGrupo) AS TBase 

답변

0

문제를 반복했다.

FechaDocumento는 DATETIME이므로 캐스트가 필요합니다.

감사합니다.

0

where 절을 다음과 같이 바꾸십시오.

WHERE  ((TM2000_RegistroGeneral.FechaDocumento >= @fi 
       AND TM2000_RegistroGeneral.FechaDocumento <= @ff) 


     OR (TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -1, @fi) 
       AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -1, @ff)) 

     OR (TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -2, @fi) 
       AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -2, @ff)) 

     OR (TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -3, @fi) 
       AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -3, @ff)) 

     OR (TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -4, @fi) 
       AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -4, @ff)) 
      ) 
      AND (ISNULL(TM2000_Asuntos.CodigoNulo, 0) = 0) 

나는 당신의 OR & 및 위치를 서로 싸우고 믿고 WHERE 절에서 비교할 때 당신은 있었다 AND 절은 CAST (DATE, TM2000_RegistroGeneral.FechaDocumento)로 해결 불필요하게