-2

我正在运行以下查询和 QoQ 。您能告诉我应该如何处理“下载 CSV”文件选项吗?

<!--- QoQ for FIRSTCONN --->

<cfquery datasource = "XX.XX.X.XX" name="master1">
     SELECT STR_TO_DATE(date_format(Timedetail,'%m-%d-%Y'),'%m-%d-%Y') as FIRSTCONN
            , COUNT(Timedetail) as FIRSTOccurances
            , EVENTS 
     FROM  MyDatabase
     WHERE EVENTS = "FIRST" 
     GROUP BY FIRSTCONN ;
</cfquery> 

<!--- Detail Query --->

<cfquery dbtype="query" name="detail1">
    SELECT  *
    FROM master1 
    WHERE FIRSTCONN  >= <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_varchar"> 
    AND   FIRSTCONN  <  <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_varchar">;
</cfquery>  


<!--- QoQ for SECONDCONN --->

<cfquery datasource = "XX.XX.X.XX" name="master2">
    SELECT STR_TO_DATE(date_format(Timedetail,'%m-%d-%Y'),'%m-%d-%Y') as SECONDCONN
           , COUNT(Timedetail) as SECONDOccurances
           , EVENTS 
    FROM  MyDatabase
    WHERE EVENTS = "SECOND" 
    GROUP BY SECONDCONN ;
</cfquery> 


<cfquery dbtype="query" name="detail2">
    SELECT  *
    FROM   master2 
    WHERE  SECONDCONN  >= <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_varchar"> 
    AND    SECONDCONN  <  <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_varchar">;
</cfquery>  


<cfchart format="flash"  chartwidth="1000" chartheight="500" scalefrom="0" scaleto="50000" xAxisTitle="Dates" yaxistitle="Number of Connections">
     <cfchartseries  query="detail1" type="line" itemColumn="FIRSTCONN" valueColumn="FIRSTOccurances" > 
     <cfchartseries  query="detail2" type="line" itemColumn="SECONDCONN" valueColumn="SECONDOccurances" > 
     </cfchartseries>
</cfchart>[/CODE]

The cfform code and cfscript code I'm using is as follows:

[CODE]<cfform format="flash" preloader ="false">


<cfformgroup type="horizontal">

  <cfinput type="dateField" name="startdate" label="Start Date" width="100" value="#form.startdate#">
  <cfinput type="dateField" name="enddate" label="End Date" width="100" value="#form.enddate#">
  <cfinput name="submitApply" type="submit" value = "Apply">
  <cfinput name="cancel" type="submit" value="Download CSV">

</cfformgroup>


<cfscript>
    var tl ='';
    var nl = (Chr( 13 ) & Chr( 10 ));
    var fileContent = createObject("java","java.lang.StringBuffer").init();
    var counter =1;
    fileContent.append( 'FIRST');
     fileContent.append(nl);
            for(i=1;i<=detail1.recordCount;i=i+1){
                tl = detail1.FIRST;
                fileContent.append(tl);
                fileContent.append(nl);
            }




  fileContent.append( 'SECOND');
     fileContent.append(nl);
            for(i=1;i<=detail2.recordCount;i=i+1){
                tl = detail2.SECOND;
                fileContent.append(tl);
                fileContent.append(nl);
            }           

</cfscript>

<cfset absoluteFilePathAndName = " C:\ColdFusion8\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\">

<cfset realtiveFilePathAndName = " C:\ColdFusion8\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\">

<cffile action="write" file="#absoluteFilePathAndName#" output="#fileContent.toString()#"/>
<a href="#realtiveFilePathAndName#>Download</a> 

期望的输出:

我附上了图片下面的输出。请在附件中找到它。

基本上,如果日期范围是 6 月 21 日至 7 月 21 日。输出必须如图所示。(为了我的代码简单起见,我省略了 THIRDCONN 等)。

我试图尝试解决上述问题,是否需要为每一列编写 fileContent.append() ?如果我错了,请告诉我。

PS 我是 CF 新手,以前没有这样做过。

谢谢

4

1 回答 1

0

使用单独的查询使这比必要的要困难得多。要使其工作,您基本上需要将每个查询的行转置或转置为单独的列。这不是一件容易的事,除非您的所有查询都保证包含完全相同的日期,以相同的顺序(不太可能)。

如果您有固定数量的事件,则使用单个数据库查询来生成所有计数要简单得多使用CASE语句根据“事件”值构建计数:

(注意:这不能在 QoQ 内完成。他们不支持CASE)。

 SELECT STR_TO_DATE(date_format(Timedetail,'%m-%d-%Y'),'%m-%d-%Y') as TheDate
        , SUM( CASE WHEN EVENTS = 'First' THEN 1 ELSE 0 END ) AS FirstConn
        , SUM( CASE WHEN EVENTS = 'Second' THEN 1 ELSE 0 END ) AS SecondConn
        , SUM( CASE WHEN EVENTS = 'Third' THEN 1 ELSE 0 END ) AS ThirdConn
 FROM  YourTableName
 WHERE Timedetail  >= <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_date"> 
 AND   Timedetail  <  <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_date">
 GROUP BY STR_TO_DATE(date_format(Timedetail,'%m-%d-%Y'),'%m-%d-%Y') 

在一个查询中获得所有结果后,您可以自己完成其余的工作。正如亚当所说,只需对 csv 的 ColdFusion 查询进行搜索。您可以遵循大量示例,以及一些用于将查询转换为 CSV 的预构建函数:

于 2013-07-22T20:14:46.010 回答