我有递归日期的sql查询错误问题
递归查询“日期”的“日历日期”列中的锚点和递归部分之间的类型不匹配。
";WITH Dates as ("
+ " SELECT @FromDate as CalendarDate "
+ " UNION ALL "
+ " SELECT dateadd(MONTH, 1, CalendarDate) AS CalendarDate "
+ " FROM Dates "
+ " WHERE DATEADD(MONTH, 1, CalendarDate) < @ToDate )"
+ " ,cteMonthEnd AS ( "
+ " SELECT* , MonthEnd = DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, CalendarDate) + 1, 0)) "
+ " FROM Dates ) "
+ " SELECT CTE.CalendarDate, s.StrainId,GOH.CagePerDiem, "
+ " COUNT(CASE WHEN DATEDIFF(DAY, M.BirthDate, CTE.MonthEnd) >= 17 and M.DeathDate IS NULL THEN 1 END) TotalKeptMicesOver17Days,"
+ " COUNT(CASE WHEN AnimalUseCd = 2 and DATEDIFF(DAY, M.BirthDate, CTE.MonthEnd) >= 17 and DeathDate IS NULL THEN 1 END) BreedingKeptMices,"
+ " COUNT(CASE WHEN AnimalUseCd = 3 and DATEDIFF(DAY, M.BirthDate, CTE.MonthEnd) >= 17 and DeathDate IS NULL THEN 1 END) ExperimentKeptMices,"
+ " COUNT(CASE WHEN AnimalUseCd = 0 and DATEDIFF(DAY, M.BirthDate, CTE.MonthEnd) >= 17 and DeathDate IS NULL THEN 1 END) AvailableKeptMices,"
+ " COUNT(CASE WHEN AnimalUseCd = 2 and DATEDIFF(DAY, M.BirthDate, CTE.MonthEnd) >= 17 and GenderCd = 2 and DeathDate IS NULL THEN 1 END) BreedingKeptFemaleCount,"
+ " COUNT(CASE WHEN AnimalUseCd = 2 and DATEDIFF(DAY, M.BirthDate, CTE.MonthEnd) >= 17 and GenderCd = 1 and DeathDate IS NULL THEN 1 END) BreedingKeptMaleCount,"
+ " COUNT(CASE WHEN AnimalUseCd = 3 and DATEDIFF(DAY, M.BirthDate, CTE.MonthEnd) >= 17 and GenderCd = 2 and DeathDate IS NULL THEN 1 END) ExperimentKeptFemaleCount,"
+ " COUNT(CASE WHEN AnimalUseCd = 3 and DATEDIFF(DAY, M.BirthDate, CTE.MonthEnd) >= 17 and GenderCd = 1 and DeathDate IS NULL THEN 1 END) ExperimentKeptMaleCount"
+ " FROM cteMonthEnd CTE"
+ " CROSS JOIN Strains S"
+ " JOIN Mice M ON M.StrainId = S.StrainId"
+ " LEFT JOIN GroupOverhead GOH on S.GroupId = GOH.GroupId"
+ " WHERE S.GroupId in (Select GroupId from Groups where OrganizationId = @groupId)"
+ " GROUP BY S.StrainId, CalendarDate,GOH.CagePerDiem"
+ " order by CalendarDate asc";
这个查询已经在存储过程中执行了但是我们有这个代码的问题将在 c# 代码中合并我得到上面提到的错误