Help with grouped query

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Help with grouped query

    Hello. The following query and output is working ok except that the values
    diplayed for #num_staff#, #num_adult#, and #num_child# are never anything other
    than 1 or 0. Could someone identify what I've been doing wrong?

    Thanks. (Code attached below, first the query and then the query output.)



    <CFQUERY NAME="get_site" DATASOURCE="#request.dsn#" DBTYPE="ODBC">
    SELECT tbl_site.*, tbl_program.program, tbl_district.district,
    tbl_state.state_name, tbl_state.state_abbrev,
    COUNT(tbl_adult.ID) AS num_adult,
    COUNT(tbl_child.ID) AS num_child,
    COUNT(tbl_staff.ID) AS num_staff
    FROM tbl_site, tbl_program, tbl_district, tbl_state,
    tbl_adult, tbl_child, tbl_staff
    WHERE tbl_site.program_id = tbl_program.ID
    AND tbl_program.state_id = tbl_state.ID
    AND tbl_program.district_id *= tbl_district.ID
    AND tbl_adult.site_ID =* tbl_site.ID
    AND tbl_child.site_ID =* tbl_site.ID
    AND tbl_staff.site_ID =* tbl_site.ID
    AND tbl_program.ID = <CFQUERYPARAM VALUE="#form.ID#"
    CFSQLTYPE="CF_SQL_INTEGER">

    GROUP BY tbl_adult.ID, tbl_child.ID, tbl_staff.ID,
    tbl_site.ID, tbl_site.site, tbl_site.site_ID, tbl_site.program_ID,
    tbl_site.district_ID,
    tbl_site.site_coord, tbl_site.site_tech,
    tbl_site.site_address1, tbl_site.site_address2, tbl_site.site_city,
    tbl_site.site_state_ID, tbl_site.site_zip, tbl_site.mailing_address1,
    tbl_site.mailing_address2, tbl_site.mailing_city, tbl_site.mailing_zip,
    tbl_site.site_phone, tbl_site.site_fax,
    tbl_site.site_email, tbl_site.driving_directions, tbl_site.notes,
    tbl_program.program, tbl_district.district,
    tbl_state.state_name, tbl_state.state_abbrev

    <CFIF IsDefined("URL.sort") AND URL.sort EQ "id">
    ORDER BY tbl_site.site_ID, tbl_site.site
    <CFELSEIF IsDefined("URL.sort") AND URL.sort EQ "program">
    ORDER BY tbl_program.program, tbl_site.site
    <CFELSEIF IsDefined("URL.sort") AND URL.sort EQ "district">
    ORDER BY tbl_district.district, tbl_site.site
    <CFELSE>
    ORDER BY tbl_site.site, tbl_site.site_ID
    </CFIF>
    </CFQUERY>



    <CFOUTPUT QUERY="get_site" GROUP="site">
    <BR>
    <TABLE BORDER=1 CELLPADDING=1 CELLSPACING=0 WIDTH=400>
    <TR><TD BGCOLOR="White" ALIGN="left" WIDTH=35%>Site</TD>
    <TD BGCOLOR="White" ALIGN="left" WIDTH=65%><SPAN
    CLASS="fixed"><B>#get_site.site#</B></SPAN></TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">Site ID</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN
    CLASS="fixed"><B>#get_site.site_id#</B></SPAN>&nbsp;</TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">Program</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN
    CLASS="fixed">#get_site.program#</SPAN>&nbsp;</TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">District</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN
    CLASS="fixed">#get_site.district#</SPAN>&nbsp;</TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">Coordinator</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN
    CLASS="fixed">#get_site.site_coord#</SPAN>&nbsp;</TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">Technologist</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN
    CLASS="fixed">#get_site.site_tech#</SPAN>&nbsp;</TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">Address</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN CLASS="fixed">#get_site.site_address1#
    <CFIF get_site.site_address2 NEQ
    ""><BR>#get_site.site_address2#</CFIF><BR>
    #get_site.site_city#, #UCase(get_site.state_abbrev)#
    #get_site.site_zip#</SPAN></TD></TR>

    <CFIF get_site.mailing_address1 NEQ ""
    OR get_site.mailing_address2 NEQ ""
    OR get_site.mailing_city NEQ ""
    OR get_site.mailing_zip NEQ "">
    <TR><TD BGCOLOR="White" ALIGN="left">Mailing Address</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN
    CLASS="fixed">#get_site.mailing_address1#
    <CFIF get_site.mailing_address2 NEQ
    ""><BR>#get_site.mailing_address2#</CFIF><BR>
    #get_site.mailing_city#, #UCase(get_site.state_abbrev)#
    #get_site.mailing_zip#</SPAN></TD></TR>
    </CFIF>

    <TR><TD BGCOLOR="White" ALIGN="left">Phone</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN
    CLASS="fixed">#get_site.site_phone#</SPAN>&nbsp;</TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">Fax</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN
    CLASS="fixed">#get_site.site_fax#</SPAN>&nbsp;</TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">Email</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN
    CLASS="fixed">#get_site.site_email#</SPAN>&nbsp;</TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">Number Enrolled Staff</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN CLASS="fixed"><CFOUTPUT
    GROUP="num_staff">#get_site.num_staff#</CFOUTPUT></SPAN>&nbsp;</TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">Number Enrolled Adult Participants</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN CLASS="fixed"><CFOUTPUT
    GROUP="num_adult">#get_site.num_adult#</CFOUTPUT></SPAN>&nbsp;</TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">Number Enrolled Child Participants</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN CLASS="fixed"><CFOUTPUT
    GROUP="num_child">#get_site.num_child#</CFOUTPUT></SPAN>&nbsp;</TD></TR>

    </TABLE>
    </CFOUTPUT>

    Captain Special Guest

  2. Similar Questions and Discussions

    1. Grouped by List
      I have some calculated numbers that create a list that are grouped by and organizational category. Category_1 has 17, 34.88, 12.01 Category_2 has...
    2. Sep Query in Grouped Query
      I need a similiar solution. I am attempting to set up a 3 level product menu where there are product categories, product subcategories, and product...
    3. How to skew grouped objects?
      Ok, here's an image of what I'm referring to in this post: Image <http://burntelectrons.org/img/text-skew.png> I'm new to Illustrator, trying it...
    4. Grouped or Attached or Seperate?
      Hello, Which is more processor friendly, to have 100 seperate models in a scene. Or have one group consisting of all 100 models. Or just have on...
    5. Grouped data / Statistics
      Hi! I have a valuelist, "Source", containing several values. The valuelist is editable for users, so i dont know how many values it containe. ...
  3. #2

    Default Re: Help with grouped query

    My guess is that you are selecting too many fields.

    How many fields are in tbl_site? Do you really need them all?
    Dan Bracuk Guest

  4. #3

    Default Re: Help with grouped query

    Yes, I do need to pull all those fields - they are actually listed manually in
    the GROUP BY section. The display page is for summary data, that my users can
    see all entered sites - with all pertinent info - in one fell swoop.

    Captain Special Guest

  5. #4

    Default Re: Help with grouped query

    If you have to display all that data, run your initial select without the count() stuff in it. Then use Q of Q to get your summary data.
    Dan Bracuk Guest

  6. #5

    Default Re: Help with grouped query

    perhaps your joins are not returning nulls?
    what db you use?

    try COUNT(ISNULL(tbl_adult.ID, 0)) AS num_adult
    kim
    kim il sung Guest

  7. #6

    Default Re: Help with grouped query

    This application runs on MS SQL Server 2000. I tried the COUNT(ISNULL(tbl_adult.ID, 0)) for all three count fields, and the results showed 1, 1 and 1 each.
    Captain Special Guest

  8. #7

    Default Re: Help with grouped query

    First of all, you probably should change the old obsolete style outer join
    syntax(=* and *=) and rewrite it using the ANSI SQL-92 LEFT or RIGHT JOIN
    syntax instead. May save you lots of heartache later.

    Also, I don't know how you would expect to see any count other than 1 for any
    of your ID counts, since you are equating tbl_site.ID to tbl_adult.site_ID AND
    tbl_child.site_ID AND tbl_staff.site_ID for each row selected, then grouping by
    these fields. How would your count ever exceed one? In other words, you are
    only going to get a distinct row when all three are the same value, unless I'm
    missing the boat here.

    Phil

    paross1 Guest

  9. #8

    Default Re: Help with grouped query

    Well, strangely enough it seems to be missing the count even when I eliminate
    tbl_child and tbl_staff entirely from the equation. Here is a simplified
    query, as shown in the debugger:

    SELECT tbl_site.*, tbl_program.program, tbl_district.district,
    tbl_state.state_name, tbl_state.state_abbrev,
    COUNT(tbl_adult.ID) AS num_adult
    FROM tbl_site, tbl_program, tbl_district, tbl_state,
    tbl_adult
    WHERE tbl_site.program_id = tbl_program.ID
    AND tbl_program.state_id = tbl_state.ID
    AND tbl_program.district_id *= tbl_district.ID
    AND tbl_adult.site_ID =* tbl_site.ID
    AND tbl_program.ID = #form.program_ID#

    GROUP BY tbl_adult.ID,
    tbl_site.ID, tbl_site.site, tbl_site.site_ID, tbl_site.program_ID,
    tbl_site.district_ID,
    tbl_site.site_coord, tbl_site.site_tech,
    tbl_site.site_address1, tbl_site.site_address2, tbl_site.site_city,
    tbl_site.site_state_ID, tbl_site.site_zip, tbl_site.mailing_address1,
    tbl_site.mailing_address2, tbl_site.mailing_city, tbl_site.mailing_zip,
    tbl_site.site_phone, tbl_site.site_fax,
    tbl_site.site_email, tbl_site.driving_directions, tbl_site.notes,
    tbl_program.program, tbl_district.district,
    tbl_state.state_name, tbl_state.state_abbrev


    ORDER BY tbl_site.site, tbl_site.site_ID


    Like the more complex, first iteration, this query properly returns both sites
    from the db, but still lists the number of adult participants for each as just
    1 (there are quite a few more than that).

    The code for the output can be reduced to this for simplification:

    <CFOUTPUT QUERY="get_site" GROUP="site">
    <BR>
    <TABLE BORDER=1 CELLPADDING=1 CELLSPACING=0 WIDTH=400>
    <TR><TD BGCOLOR="White" ALIGN="left" WIDTH=35%>Site</TD>
    <TD BGCOLOR="White" ALIGN="left" WIDTH=65%><SPAN
    CLASS="fixed"><B>#get_site.site#</B></SPAN></TD></TR>

    <TR><TD BGCOLOR="White" ALIGN="left">Number Enrolled Adult Participants</TD>
    <TD BGCOLOR="White" ALIGN="left"><SPAN CLASS="fixed"><CFOUTPUT
    GROUP="num_adult">#get_site.num_adult#</CFOUTPUT></SPAN>&nbsp;</TD></TR>

    </TABLE>
    </CFOUTPUT>

    Captain Special Guest

  10. #9

    Default Re: Help with grouped query

    ....probably because you are only getting one of each row for all of the fields
    that you are grouping by. It is hard to determine what it is that you want to
    do when you are only showing us what it is that you are trying to do.

    Phil

    paross1 Guest

  11. #10

    Default Re: Help with grouped query

    I see. I apologize for not being clearer. In essence, I want the summary page
    to display a little table with all the pertinent data for each site within the
    criteria selected by the previous page. Each table in the summary page has the
    standard name, address, etc. But I also want the table to simply display the
    number of adult participants associated with each site, as well as child
    participants and staff members. Each adult participant has a single record on
    tbl_adult, and each contains the foreign key site_ID which relates each adult
    record to one site. The same is true for both child participants and staff
    members.

    The sample I'm running has anywhere from 6-12 adults, children, and staff
    listed for each of the two sites I have in the dummy data. So, what I want to
    do is get a result that contains something like the following:

    Site A
    Number of adult participants: 6
    Number of child participants: 8
    Number of staff members: 11


    Site B
    Number of adult participants: 10
    Number of child participants: 10
    Number of staff members: 4


    I hope that makes it clearer. I am very grateful for the help.

    Captain Special Guest

  12. #11

    Default Re: Help with grouped query

    Then you should only be selecting grouping by siteID, tbl_adult.ID,
    tbl_child.ID, and tbl_staff.ID. Adding all of the other fields to the same
    query is what is messing you up.

    Just off of the top of my head, would something like this much simplified
    query do what something like what you are trying to do?

    Phil



    SELECT tbl_site.ID, tbl_program.program
    (SELECT COUNT(tbl_adult.ID)
    FROM tbl_adult
    WHERE tbl_adult.site_ID = tbl_site.ID) AS num_adult,
    (SELECT COUNT(tbl_child.ID)
    FROM tbl_child
    WHERE tbl_child.site_ID = tbl_site.ID) AS num_child,
    (SELECT COUNT(tbl_staff.ID)
    FROM tbl_staff
    WHERE tbl_staff.site_ID = tbl_site.ID) AS num_staff
    FROM tbl_site, tbl_program
    WHERE tbl_site.program_id = tbl_program.ID
    AND tbl_program.state_id = tbl_state.ID
    AND tbl_program.ID = #form.program_ID#

    paross1 Guest

  13. #12

    Default Re: Help with grouped query

    Thank you - that worked beautifully, even with more detailed versions of the query.
    Captain Special 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