我创建了一个查询,它在中执行时显示的数据与在浏览器中使用cfdump或cfoutput输出时显示的数据不同。
以下是查询:
select count(stat_id) as val, month(status_date) as mnth, year(status_date) as yr
from task_status ts
join appraisal.dbo.employee e on e.userID = ts.user_ID
where e.comp = 1
and e.dept = 2
and e.archive != 1
and ts.status_date between '2016-10-01 00:00:00' AND '2017-10-01 00:00:00'
group by month(status_date), year(status_date)
order by year(status_date), month(status_date)在Management中看到的预期结果和结果如下:
YR MNTH YR
1 10 2016
1 11 2016
9 2 2017
4 3 2017
3 4 2017
18 5 2017
6 6 2017
1 7 2017 但是,从浏览器中看到的结果是:
YR MNTH VAL
2016 1 7
2016 2 13
2016 3 5
2016 4 5
2016 5 1
2016 6 4
2016 7 2
2016 10 1
2016 11 1 任何可能导致这种情况的建议都将受到欢迎,因为我不知道为什么会有不同之处。
发布于 2017-11-02 21:33:31
编辑:
尝试将查询中的日期更改为
select count(stat_id) as val, month(status_date) as mnth, year(status_date) as yr
from task_status ts
INNER JOIN appraisal.dbo.employee e on e.userID = ts.user_ID
AND e.comp = 1
AND e.dept = 2
AND e.archive != 1
WHERE ts.status_date between '20161001' AND '20171001'
group by year(status_date), month(status_date)
order by year(status_date), month(status_date)见ISO 8601。您还可以将日期更改为'2016-10-01T00:00:00' AND '2017-10-01T00:00:00'。
我相信您的日期可能会被解释为一个字符串,该字符串被读取为YYYY,并在通过ColdFusion或JVM传递到SQL时给出了错误的范围。
=========================================================================
原版:
这更多是个人偏好的评论:
更改JOIN语法,将条件从WHERE移到JOIN中。
select count(stat_id) as val, month(status_date) as mnth, year(status_date) as yr
from task_status ts
INNER JOIN appraisal.dbo.employee e on e.userID = ts.user_ID
AND e.comp = 1
AND e.dept = 2
AND e.archive != 1
WHERE ts.status_date between '2016-10-01 00:00:00' AND '2017-10-01 00:00:00'
group by year(status_date), month(status_date)
order by year(status_date), month(status_date)当使用JOIN表时,可以想象您正在使用的数据集。当您在WHERE中指定条件时,您将创建一个大的JOIN,然后使用WHERE子句过滤出这些结果。我认为更新版本的SQL使用它们的优化器更聪明,但我知道,当条件为LEFT OUTER JOIN和WHERE时,2005年可以返回不同的结果。INNER JOIN不会有所作为,但OUTER可以。
我还更改了您的GROUP BY中的顺序。它不应该改变结果,但它更干净,更符合数据可能使用方式的分组(按年份分组,然后按年份的月份分组)。
还有一个个人偏好:我不只是使用JOIN,我喜欢添加INNER JOIN,只是为了更清楚地说明我正在做什么。
https://stackoverflow.com/questions/47074976
复制相似问题