1 Reply Latest reply: Jun 16, 2009 7:56 AM by Dan Bracuk RSS

    Q of Q Group By

    Dan Bracuk Community Member

      this query runs successfully

       

      <cfquery name="TotalWebSurvey1" dbtype="query">
      select answers answers1, col theanswer, qnumber, qtext
      from AnswerCount1, questions
      where qnum = qnumber
      and col <> '[No Answer Entered]'
      <!--- add some 0 quanity rows in case a certain answer is not selected by anyone --->
      union
      select 0 answers1, atext theanswer, qnum qnumber, qtext
      from QuestionsAnswers
      </cfquery>

       

      The field qnumber is an integer (I think) and has values of 1,2, or 3.

       

      this query also runs successfully

       

      <cfquery name="x" dbtype="query">
      select qnumber
      from TotalWebSurvey1
      </cfquery>

       

      But this one,

       

      <cfquery name="x" dbtype="query">
      select qnumber, count(qnumber) cc
      from TotalWebSurvey1
      group by qnumber
      </cfquery>

       

      Gives this error:

      Error casting an object of type to an incompatible type. This usually indicates a programming error in Java, although it could also mean you have tried to use a foreign object in a different way than it was designed.

       

       

      Am I doing something stupid or is Cold Fusion not playing nicely?  If it's relevent, this is how the qnumber field gets generated.

       

       

      <cfhttp url="something.csv" name="WebSurvey1"></cfhttp>
      <!--- each column represents a  question and each row represents a person.  Get the answer counts.  --->
      <cfquery name="WebSurvey" dbtype="query">
      select * from WebSurvey1 where q1 <> ''
      </cfquery>
      <cfquery name="AnswerCount1" dbtype="query">
      <cfloop from="1" to="3" index="i">
      select #i# qNumber, q#i# col, count(*) answers
      from websurvey
      group by qNumber, col
      union
      </cfloop>
      select 0 qnumber, '' col, 0 answers
      from websurvey
      where 1 = 2
      </cfquery>

        • 1. Re: Q of Q Group By
          Dan Bracuk Community Member

          The workaround is to change this:

           

          <cfquery name="TotalWebSurvey1" dbtype="query">
          select answers answers1, col theanswer, qnumber, qtext
          from AnswerCount1, questions
          where qnum = qnumber
          and col <> '[No Answer Entered]'
          <!--- add some 0 quanity rows in case a certain answer is not selected by anyone --->
          union
          select 0 answers1, atext theanswer, qnum qnumber, qtext
          from QuestionsAnswers
          </cfquery>

           

          to this

           

          <cfquery name="TotalWebSurvey1" dbtype="query">
          select answers answers1, col theanswer, qnumber, qtext
          from AnswerCount1, questions
          where qnum = qnumber
          and col <> '[No Answer Entered]'
          </cfquery>

           

          <!--- add some 0 quanity rows in case a certain answer is not selected by anyone --->
          <cfquery name="TotalWebSurvey2" dbtype="query">
          select 0 answers1, atext theanswer, qnum, qtext
          from QuestionsAnswers
          </cfquery>

           

          which will enable this query to run successfully.

           

          <cfquery name="x" dbtype="query">
          select qnumber, count(qnumber) cc
          from TotalWebSurvey1, TotalWebSurvey2
          where qnum = qnumber
          group by qnumber
          </cfquery>

          I'm still curious about why my original code crashed.