Get the latest records per Group By SQL
<p>I have the following table:</p>
<pre><code>CREATE TABLE orders (
    id INT PRIMARY KEY IDENTITY,
    oDate DATE NOT NULL,
    oName VARCHAR(32) NOT NULL,
    oItem INT,
    oQty INT
    -- ...
);


INSERT INTO orders
  VALUES
(1, '2016-01-01', 'A', 1, 2),
(2, '2016-01-01', 'A', 2, 1),
(3, '2016-01-01', 'B', 1, 3),
(4, '2016-01-02', 'B', 1, 2),
(5, '2016-01-02', 'C', 1, 2),
(6, '2016-01-03', 'B', 2, 1),
(7, '2016-01-03', 'B', 1, 4),
(8, '2016-01-04', 'A', 1, 3)
;
</code></pre>
<p>I want to get the most recent rows (of which there might be multiple) for each name. For the sample data, the results should be:</p>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>id</th>
<th>oDate</th>
<th>oName</th>
<th>oItem</th>
<th>oQty</th>
<th>...</th>
</tr>
</thead>
<tbody>
<tr>
<td>5</td>
<td>2016-01-02</td>
<td>C</td>
<td>1</td>
<td>2</td>
<td></td>
</tr>
<tr>
<td>6</td>
<td>2016-01-03</td>
<td>B</td>
<td>2</td>
<td>1</td>
<td></td>
</tr>
<tr>
<td>7</td>
<td>2016-01-03</td>
<td>B</td>
<td>1</td>
<td>4</td>
<td></td>
</tr>
<tr>
<td>8</td>
<td>2016-01-04</td>
<td>A</td>
<td>1</td>
<td>3</td>
<td></td>
</tr>
</tbody>
</table>
</div>
<p>The query might be something like:</p>
<pre><code>SELECT oDate, oName, oItem, oQty, ...
  FROM orders
  WHERE oDate = ???
  GROUP BY oName
  ORDER BY oDate, id
</code></pre>
<p>Besides missing the expression (represented by <code>???</code>) to calculate the desired values for <code>oDate</code>, this statement is invalid as it selects columns that are neither grouped nor aggregates.</p>
<p>Does anyone know how to do get this result?</p>