Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this solution:</p> <pre><code>SELECT a.product_group, SUBSTRING_INDEX(GROUP_CONCAT(a.manufacturer ORDER BY a.occurrences DESC SEPARATOR ':::'), ':::', 1) AS manufacturer_mode FROM ( SELECT aa.product_group, aa.manufacturer, COUNT(*) AS occurrences FROM products aa GROUP BY aa.product_group, aa.manufacturer ) a GROUP BY a.product_group </code></pre> <h1>Explanation:</h1> <p>This still uses a form of subquery, but one which executes only once as opposed to one that executes on a row-by-row basis such as in your original example.</p> <p>It works by first selecting the <code>product_group</code> id, the manufacturer, and the count of how many times the manufacturer appears for each particular group.</p> <p>The <code>FROM</code> sub-select will look something like this after execution (just making up data here):</p> <pre><code>product_group | manufacturer | occurrences --------------------------------------------------- 1 | XYZ | 4 1 | Test | 2 1 | Singleton | 1 2 | Eloran | 2 2 | XYZ | 1 </code></pre> <p>Now that we have the sub-select result, we need to pick out the row that has the maximum in the <code>occurences</code> field for each product group.</p> <p>In the outer query, we group the subselect once again by the <code>product_group</code> field, but this time, <strong><em>only</em></strong> the <code>product_group</code> field. Now when we do our <code>GROUP BY</code> here, we can use a really compelling function in MySQL called <code>GROUP_CONCAT</code> which we can use to concatenate the manufacturers together and in any order we want.</p> <pre><code>...GROUP_CONCAT(a.manufacturer ORDER BY a.occurrences DESC SEPARATOR ':::'... </code></pre> <p>What we are doing here is concatenating the manufacturers together that are grouped together per <code>product_group</code> id, the <code>ORDER BY a.occurrences DESC</code> makes sure that the manufacturer with the most appearances appears <strong><em>first</em></strong> in the concatenated list. Finally we are separating each manufacturer with <code>:::</code>. The result of this for <code>product_group</code> <code>1</code> will look like:</p> <pre><code>XYZ:::Test:::Singleton </code></pre> <p><code>XYZ</code> appears first since it has the highest value in the <code>occurance</code> field. We <em>only</em> want to select <code>XYZ</code>, so we encase the concatenation within <code>SUBSTRING_INDEX</code>, which will allow us to only pick the first element of the list based on the <code>:::</code> delimiter.</p> <p>The end result will be:</p> <pre><code>product_group | manufacturer_mode --------------------------------------- 1 | XYZ 2 | Eloran </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload