0

所以我需要找到 zCumuEnrolled 的最大数量并且只返回一次该值。这就是我到目前为止所拥有的。

SELECT Distinct(MAX(zCumuEnrolled)) as total, 
       DateName(mm,zDate) as date, 
       DatePart(yyyy,zDate) as date2
FROM vRecruitment  
Where zDate Between '2010-05-01' AND '2010-05-30' 
GROUP BY zDate

当我这样做时,我得到:

total|Date|Date2
    0| May|2010
    1| May|2010

我真正想要的只是返回总计 = 1 的记录。任何建议将不胜感激!!!提前致谢!

4

4 回答 4

2

您按完整日期分组,为了获得正确的结果,您需要按月和年分组。将所有日期设置为 XXXX-XX-01 格式,然后按此表达式分组。

dateadd(day, - datePart(day, zDate) + 1, zDate)

尝试下一个代码

SELECT 
    MAX(zCumuEnrolled) as total,
    DateName(mm, d) as date1,
    DatePart(yyyy, d) as date2
FROM 
    (select *, dateadd(day, - datePart(day, zDate) + 1, zDate) as d from vRecruitment) t
Where 
    zDate Between '2010-05-01' AND '2010-05-30' 
GROUP BY 
    d

如果您想按月获得最大值,这很有效

于 2012-04-30T20:12:32.333 回答
1

按 zDate 分组会导致结果对 zDate 的每个不同值都有记录;如果您想要整个日期范围内的最大值,请消除 GROUP BY 子句。(为什么你需要 Max 与众不同?)

于 2012-04-30T16:52:13.053 回答
1
Select Max(zCumuEnrolled) as Total From ZRecruitment 
where zDate Between '2010-05-01' AND '2010-05-30'

如果我得到你,有点令人困惑的问题。如果您想要具有该最大值的记录的月份和年份,则必须澄清。

PS最大怎么可能是总数...

于 2012-04-30T16:54:36.497 回答
0

像这样:

SELECT zCumuEnrolled, zDate
FROM vRecruitment  
Where zDate Between '2010-05-01' AND '2010-05-30' 
    AND zCumuEnrolled = (SELECT MAX(zCumuEnrolled) FROM vRecruitment)

这是你想做的吗?

于 2012-04-30T16:56:27.717 回答