Question

I'm running the following query and QoQ . Could you tell me how should I proceed for the "Download CSV" file option?

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

Desired Output:

I have attached the image for the output below. Please find it attached.

Basically, if a date range is 21June to 21 July. The output must be as shown in the image. (I have omitted THIRDCONN etc for the sake of simplicity in my code).

I tried to attempt to the above problem,Do I need to write fileContent.append() for each and every column? Please let me know if I'm wrong.

P.S. I'm new to CF and haven't done this before.

Thanks

Was it helpful?

Solution

Using separate queries makes this a LOT more difficult than necessary. For it to work, you essentially need to pivot or transpose the rows of each query into separate columns. Not an easy task, unless all of your queries are guaranteed to contain the exact same dates, in the same order (unlikely).

If you have a fixed number of events, it is much simpler to use a single database query to generate all of counts. Use a CASE statements to build the counts based on the "Events" value:

(Note: This cannot be done inside a QoQ. They do not support 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') 

Once you have all the results in a single query, you can do the rest on your own. As Adam mentioned, just do a search on ColdFusion query to csv. There are tons of examples you can follow, as well as a number of pre-built functions for converting queries to CSV:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top