Home >Database >Mysql Tutorial >How to Efficiently Find the Latest Position for Each Security in a Database?
Groupwise Maximum
The objective is to retrieve the latest position, identified by its ID and the corresponding buy date, for each unique security within a given dataset.
Consider the following table positions.
</th></tr></thead> <tbody> <tr> <td>id</td> <td>security</td> <td>buy_date</td> </tr> <tr> <td>26</td> <td>PCS</td> <td>2012-02-08</td> </tr> <tr> <td>27</td> <td>PCS</td> <td>2013-01-19</td> </tr> <tr> <td>28</td> <td>RDN</td> <td>2012-04-17</td> </tr> <tr> <td>29</td> <td>RDN</td> <td>2012-05-19</td> </tr> <tr> <td>30</td> <td>RDN</td> <td>2012-08-18</td> </tr> <tr> <td>31</td> <td>RDN</td> <td>2012-09-19</td> </tr> <tr> <td>32</td> <td>HK</td> <td>2012-09-25</td> </tr> <tr> <td>33</td> <td>HK</td> <td>2012-11-13</td> </tr> <tr> <td>34</td> <td>HK</td> <td>2013-01-19</td> </tr> <tr> <td>35</td> <td>SGI</td> <td>2013-01-17</td> </tr> <tr> <td>36</td> <td>SGI</td> <td>2013-02-16</td> </tr> <tr> <td>18084</td> <td>KERX</td> <td>2013-02-20</td> </tr> <tr> <td>18249</td> <td>KERX</td> <td>0000-00-00</td> </tr> </tbody> </table> <p> An optimized query that retrieves the desired results is as follows: SELECT p1.id, p1.security, p1.buy_date FROM positions p1 LEFT JOIN positions p2 ON p1.security = p2.security AND p1.buy_date < p2.buy_date WHERE p2.id IS NULL; This query avoids the use of sub-queries, which can significantly reduce execution time, particularly for large datasets. The result is a table with the following structure:
|
---|