Railo Query of Query返回错误的结果

时间:2013-09-17 09:33:20

标签: coldfusion railo qoq

我正在运行以下两个陈述:

首先是A)它做了它需要做和工作的事情:

SELECT
  itemColumn
 ,valueColumn
 ,label
 FROM
 rstCombinedChartData

 UNION ALL

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

这给了我以下结果:

enter image description here

接下来我需要获取这些结果,并在这种情况下返回每个值的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

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

enter image description here

这里有什么问题,查询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并不喜欢所有的肯定,没有任何答案我把其他东西放入混合物中,并且它开始正确地再次正确地应对它们。

有谁知道这里发生了什么?我已经厌倦了在sql中作为varchar进行转换,但是这不仅仅在CF级别上有效。

enter image description here

如果我拿出这条线:

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

它可以追溯到:

enter image description here

3 个答案:

答案 0 :(得分:3)

来自评论 - 我很好奇为什么当valueColumn已经是一个整数时,你必须将CAST(valueColumn AS INTEGER) AS valueColumn作为行itemColumn中的一个整数?

您应该尝试在同一个SQL语句中将CAST(itemColumn AS CHAR) AS itemColumn转换为char。注意:在MySQL中,您无法转换为VARCHAR。在MySQL中,您必须使用CHAR。类似于<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>

其余部分并非真正答案,但评论时间太长

我根据您共享的数据创建了一个自包含的repro,但我无法让它失败。您的查询在ACF和Railo中对我正常工作。复制下面的代码并将其粘贴到cflive.net

cfdump

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

Railo dump

在Adobe ColdFusion中,它将{{1}}更改为布尔值,并显示为“true”或“false”。

ACF dump

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

答案 1 :(得分:2)

这可能是范围/参考问题;

变化:

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

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

然后在QoQ之后,在local.arrChartData循环结束时,将其设置回rstCombinedChartData变量;

        <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>

这应确保正确设置所有指针/引用。

答案 2 :(得分: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;