我如何获取查询以使用url给我一个月的结果?

时间:2014-07-02 17:59:23

标签: coldfusion

以下查询有效,并为我提供了正确的结果。

<cfset month = URL.month>
<cfset year = URL.year>

<cfquery datasource="testing_1" name="allDepartments">
        SELECT CAST(employeedept AS INT) as dept,
        ROUND(AVG(case when rating1>0 THEN CAST(rating1 AS FLOAT) ELSE null END), 2) as q1,
        ROUND(AVG(case when rating2>0 THEN CAST(rating2 AS FLOAT) ELSE null END), 2) as q2,
        ROUND(AVG(case when rating3>0 THEN CAST(rating3 AS FLOAT) ELSE null END), 2) as q3,
        ROUND(AVG(case when rating4>0 THEN CAST(rating4 AS FLOAT) ELSE null END), 2) as q4,
        ROUND(AVG(case when rating5>0 THEN CAST(rating5 AS FLOAT) ELSE null END), 2) as q5

        FROM CSEReduxResponses
        WHERE execoffice_date BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
        AND DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)
        AND execoffice_status = 1   
        group by employeedept
        order by employeedept
</cfquery>

我想让execoffice_date给我一个月前的数据,这就是我的原因 使用GETDATE。但如果网址改变月份= 5&amp; year = 2014年到月份= 3&amp; year = 2014,它会给我 同时在查询中得到相同的结果。

我想根据网址月份和年份获取查询以获取数据。

我将如何做到这一点?

1 个答案:

答案 0 :(得分:3)

如果给你url.year和url.month,这将获得当前月份。

StartDate = CreateDate(url.year, url.month, 1);
EndDate = DateAdd("m", 1, StartDate);  // you really do want the 1st of next month

像这样使用它们:

and execoffice_date >= <cfqueryparam cfsqltype="cf_sql_date" value="#StartDate#">
and execoffice_date < <cfqueryparam cfsqltype="cf_sql_date" value="#EndDate#">

前几个月,请使用日期添加这些变量。