• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Access database dynamic query output...

Explorer ,
Sep 07, 2010 Sep 07, 2010

Copy link to clipboard

Copied

Dear CF Colleagues:

I need to run a loop for 4 times, to access four seperate values out of a data row in a test results table (tbl_results), where all four are in the row of the student (WHERE studentid = etc).

It looks like this...

  <cfset variables.TimeTotals = 0>

  <cfloop from="1" to="4" step="1" index="timrec">

     <cfquery datasource="mydsn" name="rs_time">

      SELECT Btime#timrec#

      FROM tbl_results

      WHERE studentid = 'jack';

     </cfquery>

<cfset variables.TimeTotals = variables.TimeTotals + (????? rs_time.Btime#timrec# value ?????)>

</cfloop>

*** QUESTION: How do I address that Btime#timrec# value? Not in this way, I know, but how?

Looking forward to your wisdom, with gratitude,

Hans

TOPICS
Advanced techniques

Views

594

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Sep 07, 2010 Sep 07, 2010

There are a couple of ways to attack this, but you're pretty close.  One way is to just include a column alias in the query, then use it to reference the results:

<cfset variables.TimeTotals = 0>

  <cfloop from="1" to="4" step="1" index="timrec">

     <cfquery datasource="mydsn" name="rs_time">

      SELECT Btime#timrec# AS thistime

      FROM tbl_results

      WHERE studentid = 'jack';

     </cfquery>

<cfset variables.TimeTotals = variables.TimeTotals + timerec.thistime>

</cfloop>

Votes

Translate

Translate
Enthusiast ,
Sep 07, 2010 Sep 07, 2010

Copy link to clipboard

Copied

There are a couple of ways to attack this, but you're pretty close.  One way is to just include a column alias in the query, then use it to reference the results:

<cfset variables.TimeTotals = 0>

  <cfloop from="1" to="4" step="1" index="timrec">

     <cfquery datasource="mydsn" name="rs_time">

      SELECT Btime#timrec# AS thistime

      FROM tbl_results

      WHERE studentid = 'jack';

     </cfquery>

<cfset variables.TimeTotals = variables.TimeTotals + timerec.thistime>

</cfloop>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 07, 2010 Sep 07, 2010

Copy link to clipboard

Copied

Dear Reed:

Thanks so much! That worked 100%. Never knew this trick, but it surely is a very very helpful one!

Thanks again!

Hans

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 07, 2010 Sep 07, 2010

Copy link to clipboard

Copied

As Reed says, many ways to tackle this.

I would also point out the ability to ready all the BTIme... columns with one query then loop through the columns to total that time.... OR total them in the database.

These both have the advantage of only needing one network connection to the database, which is the slowest part of this process.

A)

<cfquery name rs_time ...>

   SELECT Btime1, Btime2, Btime3 Btime4

  ...

</cfquery>

<cfset variable.TimeTotals = 0>

<cfloop list="#rs_time.columnList#" index="col">

     <cfif left(col,5) EQ "BTIME"><cfset variables.TimeTotals = variable.TimeTotals + rs_time[col][1]>

</cfloop>

B)

<cfquery name="rs_time"...>

     SELECT Btime1 + Btime2 + Btime2 + Btime4 AS totalTime

     ...

</cfquery>

OF COURSE

I would be remis if I did not point out that you would have a more expandable database design if you normalized this so that the test times where not columns in the tbl_resluts table but rather rows.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 07, 2010 Sep 07, 2010

Copy link to clipboard

Copied

LATEST

Excellent input. Thanks for this, Ilssac!

I agree that the list of columns is less preferable and less normalized than having the data stored in rows. However, we have a specific reason for doing it this way. I considered the logical alternative, but it creates more issues in the results querying.

Very grateful, though, for your demo on how to solve this challenge. I've now gained extra tools for my database toolbox.

Thanks!

Hans

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation