Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>SELECT col1, MAX(col3) AS col3, MIN(col2) AS col2, MAX(col4) AS col4 FROM MyTable GROUP BY col1; </code></pre> <p>You showed in your example that you wanted a <code>col4</code> included, but you didn't say which value you want. You have to put that column either in an aggregate function or in the <code>GROUP BY</code> clause. I assumed that taking the max for the group would be acceptable.</p> <hr> <p><strong>update:</strong> Thanks for the clarification. You're asking about a variation of the greatest-n-per-group problem that comes up frequently on Stack Overflow. Here's my usual solution:</p> <pre><code>SELECT t1.* FROM mytable t1 LEFT OUTER JOIN mytable t3 ON t1.col1 = t3.col1 AND t1.col3 &lt; t3.col3 WHERE t3.col1 IS NULL; </code></pre> <p>In English: show me the row (<code>t1</code>) for which no row exists with the same <code>col1</code> and a greater value in <code>col3</code>. Some people write this using a <code>NOT EXISTS</code> subquery predicate, but I prefer the <code>JOIN</code> syntax.</p> <p>Here's the output from my test given your example data:</p> <pre><code>+------+------+------+------+ | col1 | col2 | col3 | col4 | +------+------+------+------+ | 1 | 2 | 1 | a | | 1 | 3 | 1 | a | | 2 | 4 | 1 | b | | 3 | 5 | 0 | c | +------+------+------+------+ </code></pre> <p>Notice that there are two rows for <code>col1</code> value 1, because both rows satisfy the join condition; no other row exists with a <em>greater</em> value in <code>col3</code>.</p> <p>So we need to add another condition to resolve the tie. You want to compare to rows with a <em>lesser</em> value in <code>col2</code> and if no such rows exist, then we've found the row with the least value in <code>col2</code>.</p> <pre><code>SELECT t1.* FROM MyTable t1 LEFT OUTER JOIN MyTable t3 ON t1.col1 = t3.col1 AND t1.col3 &lt; t3.col3 LEFT OUTER JOIN MyTable t2 ON t1.col1 = t2.col1 AND t1.col3 = t2.col3 AND t1.col2 &gt; t2.col2 WHERE t2.col1 IS NULL AND t3.col1 IS NULL; </code></pre> <p>Here's the output from my test given your example data:</p> <pre><code>+------+------+------+------+ | col1 | col2 | col3 | col4 | +------+------+------+------+ | 1 | 2 | 1 | a | | 2 | 4 | 1 | b | | 3 | 5 | 0 | c | +------+------+------+------+ </code></pre> <p>PS: <em>By the way, it's customary on Stack Overflow to edit your original question and add detail, instead of adding answers to your own question that only clarify the question. But I know some actions aren't available to you until you have more than 1 reputation point.</em></p>
    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