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

  1. #1

    Default Group By question

    Scores Table:

    name / score / gameID

    mike....234.....8
    dina.....534.....8
    john.....463.....6
    tom......742.....7
    kim......653.....7

    How do I get the highest scorer from each game:

    dina.....534.....8
    john.....463.....6
    tom......742.....7

    As far as I got:

    select max(score), gameID
    from scores
    group by gameID

    Don't know how to conserve the name during the grouping.

    Feel like this is basic, but can't figure this out. I tried all kinds of
    queries.

    sblue Guest

  2. Similar Questions and Discussions

    1. "group by" - order of rows in group
      this is simple example: TABLE SCHEMA: create table my_table( id int unsigned not null auto_increment primary key, project varchar(255) not...
    2. May 29 Sydney Developers Group study group
      On Monday 29th May, we'll be studying Actionscript 3. Please read the articles prior to the meeting (see <a target=_blank...
    3. cfgrid inside a <cfoutput query="myQuery" group="GROUP">
      Is it possible to use a cfgrid inside a cfoutput with a query and a group. When I try do that I get the following error: INVALID_CHARACTER_ERR:...
    4. Question for this Group ... dont flame me :)
      Hello Jeff, These answers are of course my own experience, but may be significant to understand the bigger picture. I reorder your points a bit...
    5. Volume Group Migration Question
      Hello all, We have a p6000 e server running AIX 5.1.0.0 patch 5100-03. We have a large Volume Group consisting of multiple smaller logical volumes...
  3. #2

    Default Re: Group By question

    I haven't thought this through very well, and I think that you may have
    problems with games where you have tie scores, but this this is off the top of
    my head.

    select distinct s1.game_id, s1.name, s1.score
    from scores s1
    where s1.score = (select max(s2.score)
    from scores s2
    where s2.game_id = s1.game_id)

    Phil

    paross1 Guest

  4. #3

    Default Re: Group By question

    Ok, that works out, but here is the thing. The SCORES table is really a complex
    derived table, and since your solution uses it twice I have two issues:

    1) the overall code becomes almost unreadably long if I simply replace SCORES
    with the derived table code
    2) will performance be affected by deriving the table twice?

    I have a feeling you will suggest using views, any other approaches?

    sblue Guest

  5. #4

    Default Re: Group By question

    So Scores is a sub-query? Make it a Cold Fusion query and do a Q of Q, with
    the appropriate order by clause, and maxrows = 1. Doesn't solve the problem of
    ties though.

    Originally posted by: sblue
    Ok, that works out, but here is the thing. The SCORES table is really a
    complex derived table, and since your solution uses it twice I have two issues:

    1) the overall code becomes almost unreadably long if I simply replace SCORES
    with the derived table code
    2) will performance be affected by deriving the table twice?

    I have a feeling you will suggest using views, any other approaches?



    Dan Bracuk Guest

  6. #5

    Default Re: Group By question

    Views are OK for certain circumstances, but they usually make things easier for
    the programmer, not more efficient for the database engine. Without seeing your
    code, it?s hard to comment on how selecting this "table" twice will negatively
    affect your performance. Most of that has to do with how well your code is
    optimized, your use of indexes, etc. You should avoid doing things like using
    function calls on fields within the WHERE clause (like data conversion
    functions, etc.) because that usually negates the use of indexes on those
    fields, etc. I might be able to offer suggestions if you were to include your
    derived table, or your original query, unless it is prohibitively large.

    Good luck,
    Phil

    paross1 Guest

  7. #6

    Default Re: Group By question

    Paross,

    Using your example, I was able to come up with something that worked out well. Thanks for all the advice!
    sblue 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