Cfquery table group by

Discussion in 'Programming' started by kimimaro, Feb 23, 2009.

  1. #1
    Hi i'll try to simplified my question

    I am not good in SQL and im in training of using CFM

    I want to do a table which calls from a SQL


    <cfquery name="qjoin" datasource="#application.fw.config.dsn#">
    SELECT *
    FROM academic_subject x, subject s
    where x.subject = s.subject
    </cfquery>



    It gives out the result below (Please ignore the language)


    [​IMG]

    SRP/PMR (setaraf LCE) <1st column> Bahasa Malaysia <2nd column>
    SRP/PMR (setaraf LCE) <1st column> Bahasa Inggeris <2nd column>
    SRP/PMR (setaraf LCE) <1st column> Matematik <2nd column>
    SRP/PMR (setaraf LCE) <1st column> Ilmu Sains <2nd column>
    Ijazah Sarjana <1st column> Pengajian Am <2nd column>




    Basically the query above links 2 tables from the database.

    It display correctly in the table im fitting them in but problem is.. as you can see the repeated "SRP/PMR (setaraf LCE)" or any future input is annoying. I wanted to do a cfm table in which the column with the same name is rowspan as 1. Something like

    [​IMG]

    SRP/PMR (setaraf LCE) <1st column> Bahasa Malaysia <2nd column>
    <1st column> Bahasa Inggeris <2nd column>
    <1st column> Matematik <2nd column>
    <1st column> Ilmu Sains <2nd column>
    Ijazah Sarjana <1st column> Pengajian Am <2nd column>




    i tried alot of methods from Distinct to group by and use cfloop and cfoutput query but all fail. Are there any simple way to do this? Thanks
     
    kimimaro, Feb 23, 2009 IP
  2. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The simplest option is to use "group", but put the second set of values on separate lines, not table cells.

    <table border="1">
    <cfoutput query="yourQuery" group="FirstColumn">
    <tr>
    <td>#FirstColumn#</td>
    <td><cfoutput>
    #SecondColumn#<br>
    </cfoutput>
    </td>
    </tr>
    </cfoutput>
    </table>

    To use separate cells you would have to calculate the "rowspan" of the first cell ahead of time. It could be done with a QoQ or some nested loops. But not as simply as the first option.
     
    cfStarlight, Feb 25, 2009 IP
  3. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    BTW, when using "group" you must ORDER BY that column or the cfoutput will not work correctly.
     
    cfStarlight, Feb 25, 2009 IP