Skip to main content
Inspiring
April 1, 2013
Question

Many line by line calculations

  • April 1, 2013
  • 7 replies
  • 1262 views

Hi All,

I have a component with couple methods, each method is the result of query from the database.

I want to do some calculations after i get the data from the server. I am lokking for a solution with good performance because I have many calculations to do.

Code:

<cfset myObj = CreateObject("component", "cfcs.myComponent") />

<cfset firstQry = myObj.getFirstResult(arguments.A, arguments.B) />

<cfset secondQry = myObj.getSecondResult(arguments.A, arguments.B) />

Both query results have the same structure myID, myDesc, year_1, year2, year3

firstQry have 10 rows, how can I sum the years row 3 (myID = 3) and years row 5 (myID = 5) ?

and add the result of the calculation into firstQry as line 11 (new myID = 11).

secondQry have one row result.

How can I multiple the firstQry years row 4 (myID=4) with the secondQry years row 1 (only row myID = 1)? 

and add the result into firstQry as line 12 (new myId =12).

I was thinking to use query of query but I wondering if it is a better way to do this.

Thanks in advance for your help.

Best,

    This topic has been closed for replies.

    7 replies

    Inspiring
    April 1, 2013

    Q of Q seems as good a way as any.

    jfb00Author
    Inspiring
    April 1, 2013

    Q of Q have many limitations. I have to generate a lot of code.

    If I want to do the first calculation, i have to create another query because I cannot use query table alias like:

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

    select * from firstQry

    </cfquery>

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

    select 11 as myID, 'cal 12' as myDesc

    <cfloop from="1" to="3" index="y">

    ,(firstQry.year_#y# + firstQry2.year_#y#) as year_#y#

    </cfloop>

    from firstQry, firstQry2

    where firstQry.myID = 3

    and firstQry2.myID = 5

    </cfquery>

    Is this correct, or I can do this in a better way?

    Now, how can i insert the result into firstQuery?

    I try to do another Q of Q with insert but I am getting an error.

    Query Of Queries syntax error.
      Encountered "insert. 

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

    insert into firstQuery (myID, myDesc

    <cfloop from="1" to="3" index="y">

    ,year_#y#

    </cfloop>

    )

    select  myID, myDesc

    <cfloop from="1" to="3" index="y">

    ,year_#y#

    </cfloop>

    from qryResult

    </cfquery>


    BreakawayPaul
    Inspiring
    April 2, 2013

    Have you considered putting the query inside the loop rather than the reverse?

    <cfloop from="1" to="3" index="y">

    <cfquery  name="insertQry" datasource="myDsn">

    INSERT INTO firstQuery (myID, myDesc, year_#y#)

    VALUES (...

    </cfquery>

    </cfloop>

    I don't think you can use a QoQ to insert (or update) records.  AFAIK you're just working with a cached copy of the data, so updating that doesn't really make sense.