CFC Query - Should be easy!

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default CFC Query - Should be easy!

    In MySQL we have the ability to process the following:

    SELECT @n := COUNT(something) FROM sometable

    and then use that result in another query to generate a percentage:

    SELECT something, (COUNT(something)*100/@n AS percent FROM sometable GROUP BY
    something

    I know this is probably very easy to accomplish using a CFC. I would guess
    that one function in the CFC would call another function in the CFC, each
    containing the appropriate query, but I'm missing something...

    <!--- GridFill() method --->
    <cffunction name="gridFill" returntype="query" hint="retrieve data to
    populate a grid">

    <!--- make call to function within this component --->
    <!--- <cfinvoke
    component="cf.cfc.missionDb"
    method="getRecords"
    returnvariable="getRecordsRet">
    </cfinvoke> --->

    <!--- var scoped variables --->
    <cfset var getList = "">

    <!--- run the query --->
    <cfquery name="getList" datasource="bag">
    SELECT svcRegion, (COUNT(svcRegion)*100)/3 AS percent FROM m_name GROUP
    BY svcRegion
    </cfquery>
    <!--- send the results --->
    <cfreturn getList>
    </cffunction>

    <!---
    function getRecords
    Generated by the CFC Recordset ColdFusion MX Extension
    Sun Jul 24 16:13:14 GMT-0400 (Eastern Daylight Time) 2005
    --->
    <cffunction name="getRecords" output="false" access="public"
    returntype="query">
    <cfset var recCount = "" >
    <cfquery name="recCount" datasource="bag">
    SELECT Count(*) as rec_Count FROM m_name
    </cfquery>
    <cfreturn recCount>
    </cffunction>

    The highlighted '3' in '(COUNT(svcRegion)*100)/3 AS percent ' above would be
    replaced by variable in 'recCount'. Alas, it's not working. I'm sure that
    it's something quite simple, but I'm stumped...

    RonKochanowski Guest

  2. Similar Questions and Discussions

    1. making easy the CFC Query behavior in Dreamweaver
      hello. make a copy of your cfc query, then replace the code of cfcquery for this: <cfscript> obj = createobject("component","@@CfcName@@");...
    2. Query of Queries on query New type query
      In CF5 we have a page that creates a query, using queryNew and querySetCell and the like, we then used dbtype="query" and gave it's name so we could...
    3. Easy question = easy answer?
      Well, so I'm pretty new with Freehand, at the mo running with MX and havin' a problem (not big, but anyway)... I'm creating some cards and they...
    4. Easy Question/Easy Answer
      Ok, this is all i want to know how to do, i made a text link, now when someone rolls over it with their pointer i want it to change color. Simple?
    5. BCP query out executed by xp_cmdshell works fine from query analyzer but fails from VB Component
      Hi all, I have a stored procedure which returns a vast number of record and i have to write the output into a csv file. I'm using BCP utility to...
  3. #2

    Default Re: CFC Query - Should be easy!

    First replace this line:
    <!--- run the query --->
    with this:
    <!--- Run the query. IMPORTANT: If getRecordsRet does not return EXACTLY one
    row, then an error (or bad results) will occur. --->

    Then change this line:
    SELECT svcRegion, (COUNT(svcRegion)*100)/3 AS percent FROM m_name GROUP BY
    svcRegion
    to this:
    SELECT svcRegion, (COUNT(svcRegion)*100)/#getRecordsRet.rec_Count# AS percent
    FROM m_name GROUP BY svcRegion

    Regards,
    -- MikeR


    MikerRoo Guest

  4. #3

    Default Re: CFC Query - Should be easy!

    Thanks for the response Mike. Your suggestion was one of the things that I had
    tried before posting... it didn't work. And now I'm even more confused and
    frustrated. So, I will post the code that I'm putting together, both the CFC
    and the CFM that calls it:



    <!---
    Filename: missionDb.cfc
    Author: Ron Kochanowski
    Purpose: component used to access the missions tables for data input and
    retrieval
    --->

    <!--- define the CFC --->
    <cfcomponent hint="Provides a means to enter, edit and retrieve data for
    missions from the database">

    <!---
    function getRecords
    Generated by the CFC Recordset ColdFusion MX Extension
    Sun Jul 24 16:13:14 GMT-0400 (Eastern Daylight Time) 2005
    --->
    <cffunction name="getRecords" output="false" access="private"
    returntype="query"
    hint="a method that returns a query object containing the number of records
    in the table.">

    <!--- establish the variable to hold the returned info --->
    <cfset var recCount = "" >

    <!--- run the query --->
    <cfquery name="recCount" datasource="bag">
    SELECT Count(*) as rec_Count FROM m_name
    </cfquery>
    <!--- return the results --->
    <cfreturn recCount>
    </cffunction>

    <!--- GridFill() method --->
    <cffunction name="gridFill" returntype="query" hint="retrieve data to
    populate a grid">

    <!--- var scoped variables --->
    <cfset var getList = "">

    <!--- make call to function within this component --->
    <cfinvoke method="getRecords" returnvariable="getRecordsRet"></cfinvoke>
    <cfset var regionCount = getRecordsRet.rec_Count>
    <cfoutput>#regionCount#</cfoutput>

    <!--- run the query --->
    <cfquery name="getList" datasource="bag">
    SELECT svcRegion, (COUNT(svcRegion)*100)/3 AS percent FROM m_name GROUP
    BY svcRegion
    </cfquery>
    <!--- send the results --->
    <cfreturn getList>
    </cffunction>

    </cfcomponent>


    <!--- missions_summary.cfm -- dreamweaver template info removed --->
    <!--- make connection to the cfc method that works the form --->
    <cfinvoke component="cf.cfc.missionDb" method="gridFill"
    returnvariable="gridFillRet"></cfinvoke>

    <!--- create the form --->
    <cfform name="summaryForm" preloader="yes" format="flash" width="400"
    height="400" skin="haloblue">
    <cfformgroup type="accordion">
    <cfformgroup type="page" label="Supported Missionaries by Region">
    <cfgrid name="byRegion" query="gridFillRet" rowheaders="no">
    <cfgridcolumn name="svcRegion" header="Region" />
    <cfgridcolumn name="percent" header="Percentage" />
    </cfgrid>
    </cfformgroup>
    <cfformgroup type="page" label="Missionary Support by Ministry">
    <cfgrid name="byMinistry" rowheaders="no">
    <cfgridcolumn name="ministry" header="Ministry" />
    <cfgridcolumn name="percent" header="Percentage" />
    </cfgrid>
    </cfformgroup>
    </cfformgroup>
    </cfform>

    RonKochanowski Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139