title: MySQL Group By With Case Order
body: <p>I need to group only certain rows together. Specifically, anything that is the Shipping product category needs to be grouped together by their item number. My query is:</p>
  <pre><code>SELECT
              
              imcatfil_sql.prod_cat,
              imitmidx_sql.item_no
          FROM
              estimate
          JOIN 
              imitmidx_sql ON estimate.item_no=imitmidx_sql.item_no 
          JOIN 
              imcatfil_sql ON imitmidx_sql.prod_cat=imcatfil_sql.prod_cat
          WHERE
              budget_header_id=19303 AND (deleted_by_id=0 OR deleted_by_id IS NULL) AND estimate.item_no NOT IN('157','156','158') AND hidden=0 AND item_desc_1 NOT LIKE '%SALES TAX%' 
          GROUP BY CASE WHEN imitmidx_sql.prod_cat='200' THEN estimate.item_no ELSE RAND() END;
          ORDER BY
              imcatfil_sql.prod_cat,imitmidx_sql.user_def_fld_3,estimate.id;
  </code></pre>
  <p>While everything with a prod_cat of 200 is grouped together by item_no, everything else is not in prod_cat order as specified by my ORDER BY. I suspect it's because of the ELSE RAND(). So, I took that out. What I wind up with is prod_cat 200 (i.e. Shipping) working properly but I get back a single row for everything else.</p>