5

我正在运行以下两个语句:

首先是 A),它做了它需要做的事情并且可以工作:

SELECT
  itemColumn
 ,valueColumn
 ,label
 FROM
 rstCombinedChartData

 UNION ALL

 SELECT
 itemColumn
 ,CAST(valueColumn AS INTEGER) AS valueColumn
 ,label
 FROM
 rstChartData  

这给了我以下结果:

在此处输入图像描述

接下来我需要获取这些结果并在这种情况下为每个值取回 itemcolumn 的总数是和否,即

i.e.
Yes 200
No 400 

B)这是我必须实现的查询:

SELECT
itemColumn
,SUM(valueColumn) AS valueColumn
,label
FROM
rstCombinedChartData (this is above result set)
GROUP BY
label
,itemColumn
ORDER BY
label DESC
,itemColumn DESC

但是,我得到以下不正确的结果:

在此处输入图像描述

查询 B 发生了什么事,它应该是 yes = x 和 no = x,而不是我得到的是假的和所有的总数?

CF功能:

    <cffunction name="getAverageChartData" hint="I return the data required to render an average chart." returntype="array" output="false">
        <cfargument name="surveyList" hint="I am a record set of Surveys." required="true" type="query" />
        <cfargument name="filter" hint="I am the optional filter which is to be applied to all results." required="false" default="" type="string" />

        <cfset var local=structNew() />

        <cfset var rstChartData="" />
        <cfset var rstChartDataTotal="" />
        <cfset var rstCombinedChartData=queryNew("itemColumn,valueColumn,label","varchar,integer,varchar") />

        <cfset local.objQuestion=objQuestionService.get(arguments.surveyList.question_ID[1]) />
        <cfset local.intQuestionTypeID = local.objQuestion.getTypeID() />

        <cfset local.strSubQuestionList=local.objQuestion.getAnswer() />
        <cfset local.strPossibleAnswerList=local.objQuestion.getPossibleAnswer() />

        <cfset local.arrChartDataResult=arrayNew(1) />

<!--- loop over each school's survey --->
        <cfloop query="arguments.surveyList">
            <cfset local.arrChartData = getChartData(arguments.surveyList.survey_id, arguments.surveyList.question_id, arguments.filter) />

<!--- loop over each sub question and append (union) it to a running total --->
            <cfloop array="#local.arrChartData#" index="rstChartData">
                <cfquery name="rstCombinedChartData" dbtype="query">
                    SELECT
                         itemColumn
                        ,valueColumn
                        ,label
                    FROM
                        rstCombinedChartData

                    UNION ALL

                    SELECT
                         itemColumn
                        ,CAST(valueColumn AS INTEGER) AS valueColumn
                        ,label
                    FROM
                        rstChartData
                </cfquery>
            </cfloop>
        </cfloop>


<!--- get the totals for each itemColumn --->
            <cfquery name="rstChartDataTotal" dbtype="query">
                SELECT
                     itemColumn
                    ,SUM(valueColumn) AS valueColumn
                    ,label
                FROM
                    rstCombinedChartData
                GROUP BY
                     label
                    ,itemColumn
                ORDER BY
                     label DESC
                    ,itemColumn DESC
            </cfquery>

好的 - 主要更新

我不知道为什么,但我错误地添加了这一行:

<cfset querySetCell(rstCombinedChartData, "itemColumn", "1") />

突然之间,查询开始在 Railo 中起作用!好的,现在我有一个额外的结果,“1”没有意义,但是 WTF!似乎Railo 不喜欢所有是的,没有答案我将其他东西放入混合中,它开始再次正确地踩它,就像它应该的varchars 一样。

有人知道这里发生了什么吗?我已经厌倦了将 sql 转换为 varchar 但这仅在 CF 级别不起作用,确实发生了一些事情。

在此处输入图像描述

如果我取出线路:

<cfset querySetCell(rstCombinedChartData, "itemColumn", "1") />

它可以追溯到:

在此处输入图像描述

4

3 回答 3

3

从评论中- 我很好奇为什么当它已经是一个整数时你必须将它valueColumn作为一个整数在行中转换?CAST(valueColumn AS INTEGER) AS valueColumn

您应该尝试itemColumn在同一 SQL 语句中将您的转换为 char。注意:在 MySQL 中,您不能强制转换为 VARCHAR。在 MySQL 中,您必须使用 CHAR。类似的东西CAST(itemColumn AS CHAR) AS itemColumn

其余的不是真正的答案,但评论太长了

我根据您共享的数据创建了一个独立的复制品,但我不能让它失败。您的查询在 ACF 和 Railo 中对我来说是正确的。复制下面的代码并将其粘贴到cflive.net中。

<cftry>
<cfset rstCombinedChartData = QueryNew("itemColumn,valueColumn,label","varchar,integer,varchar") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "Yes") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 33) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "No") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 45) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "Yes") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 0) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "No") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 0) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "Yes") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 72) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "No") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 66) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "Yes") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 0) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "No") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 0) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "Yes") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 42) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "No") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 38) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "Yes") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 64) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "No") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 83) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "Yes") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 0) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "No") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 0) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "Yes") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 65) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "No") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 43) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "Yes") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 0) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfset temp = QueryAddRow(rstCombinedChartData) />
<cfset temp = QuerySetCell(rstCombinedChartData, "itemColumn", "No") />
<cfset temp = QuerySetCell(rstCombinedChartData, "valueColumn", 0) />
<cfset temp = QuerySetCell(rstCombinedChartData, "label", "") />
<cfquery name="rstChartDataTotal" dbtype="query">
    SELECT
        itemColumn
        ,SUM(valueColumn) AS valueColumn
        ,label
    FROM
        rstCombinedChartData
    GROUP BY
        label
        ,itemColumn
    ORDER BY
        label DESC
        ,itemColumn DESC
</cfquery>
<html>
    <head><title>Test</title></head>
    <body>
        <h3>Test</h3>
        <div>
            <cfoutput query="rstChartDataTotal">
            <p>#rstChartDataTotal.itemColumn# - #rstChartDataTotal.valueColumn#</p>
            </cfoutput>
            <cfdump var="#rstCombinedChartData#" label="rstCombinedChartData" />
            <hr />
            <cfdump var="#rstChartDataTotal#" label="rstChartDataTotal" />
        </div>
    </body>
</html>
<cfcatch type="any">
    <cfdump var="#cfcatch#" />
</cfcatch>
</cftry>

您可以在cfdump结果中rstChartDataTotal看到 Railo 将 维护itemColumn为 varchar 并且仍然显示“是”或“否”。

铁路转储

在 Adob​​e ColdFusion 中,它将 更改itemColumn为布尔值并显示为“真”或“假”。

ACF 转储

我相信异常仅来自cflive.net生成的输出,但在这两种情况下,查询仍然运行并正确计算了两列的结果?

于 2013-09-19T13:11:00.147 回答
2

This may be a scoping/ reference issue;

Change:

<cfquery name="rstCombinedChartData" dbtype="query">

Into

<cfquery name="local.q" dbtype="query">

Then after the QoQ, at the end of the local.arrChartData loop, set it back to the rstCombinedChartData variable;

        <cfloop array="#local.arrChartData#" index="rstChartData">
            <cfquery name="local.q" dbtype="query">
                SELECT
                     itemColumn
                    ,valueColumn
                    ,label
                FROM
                    rstCombinedChartData

                UNION ALL

                SELECT
                     itemColumn
                    ,CAST(valueColumn AS INTEGER) AS valueColumn
                    ,label
                FROM
                    rstChartData
            </cfquery>
            <cfset rstCombinedChartData = local.q />
        </cfloop>
    </cfloop>

This should make sure all pointers/ references are set correctly.

于 2013-09-20T11:20:59.880 回答
0

试试这个查询 -

SELECT UNIQUE itemColumn
      , SUM( SELECT valueColumn 
             FROM   rstCombinedChartData 
             WHERE itemColumn = "yes") as YES
      , SUM ( SELECT valueColumn 
             FROM   rstCombinedChartData 
             WHERE  itemColumn = "no") as NO
      , label;
于 2013-09-17T09:48:11.780 回答