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

  1. #1

    Default HELP WITH GROUP BY

    This should be easy but can't seem to figure it out today.

    Have a table with three columns and corresponding data:
    ID / prod_desc / flag_id

    100 / JB / 0
    100 / JB / 1
    100 / JB / 2
    101 / ME / 3
    102 / QU / 4

    I need the output to read:
    100 / JB / <any JB record, can be 0, 1 or 2>
    101 / ME / 3
    102 / QU / 4

    Can't GROUP BY because of flag_id column. Tried DISTINCT and self-join. Can't
    figure out.

    Help is appreciated, thanks in advance!


    drforbin1970 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. group
      I've made a group called everyone. It has its own Sub-directory, "/home/everyone". I have 3 users in the group. Do I have to do anything else so...
    5. Hi to the group
      X-No-Archive: Yes Hi, Been lurking for the most part and I thought posting every now and then, but realized that I had been throwing those out...
  3. #2

    Default Re: HELP WITH GROUP BY

    Originally posted by: drforbin1970


    I need the output to read:
    100 / JB / <any JB record, can be 0, 1 or 2>
    101 / ME / 3
    102 / QU / 4




    Do you need ONLY the ME/ 3 and QU/4 records?

    SELECT TOP 1 * FROM Mytable WHERE prod_desc = 'JB'
    UNION
    SELECT * from Mytable where (Prod_desc = 'ME' and flag_id = 3) or (prod_desc
    = 'QU' and flag_id = 4)



    philh Guest

  4. #3

    Default Re: HELP WITH GROUP BY

    Those values are not known beforehand, need to group the data somehow.
    drforbin1970 Guest

  5. #4

    Default Re: HELP WITH GROUP BY

    If you don't need the flagid, don't select it. Also use the keyword distinct in your select clause. You won't have to group.
    Dan Bracuk Guest

  6. #5

    Default Re: HELP WITH GROUP BY

    Alternatively, you could use MAX or MIN aggregate functions for this:

    select MIN( ID ), prod_desc, MIN(flag_id)
    from theTable
    group by prod_desc

    healey_mark 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