Home > Database > Mysql Tutorial > How can I efficiently retrieve the latest entry for each group in a database table?

How can I efficiently retrieve the latest entry for each group in a database table?

Barbara Streisand
Release: 2025-01-25 19:53:09
Original
786 people have browsed it

How can I efficiently retrieve the latest entry for each group in a database table?

The latest entry of each group

The goal is to obtain the latest entry of each unique group from a database table like

. This table contains a series of document status change records, each document is identified by

. The task is to extract the latest update status of each document and its association date. DocumentStatusLogs DocumentID Consider the standardization of the database

First of all, we need to consider whether the database design has been correctly standardized. Should status information be stored in the parent table or the sub -table? If you only need to access the current state of each document, it is best to add the status field directly to the parent table to avoid using a separate table to track the status change. However, if you need to retain the historical record of state update, it is more advantageous to maintain the state change in a separate table.

Retrieve the top entry

Back to the initial problem, there is no built -in polymer function to directly retrieve the top entry of each group. Therefore, additional queries or sub -queries need to be achieved to achieve the required results. Inquiry CTE

One method is to create a public expression formula (CTE) to generate intermediate results. CTE named enhances the original table by adding a line number to each entry. This line number is generated in each group, and is arranged according to the

descending order.

This query selects the line with the line number (RN) equal to 1 from the table, effectively providing the top entry of each

grouping. cte DocumentID Alternative method DateCreated

<code class="language-sql">WITH cte AS
(
   SELECT *,
         ROW_NUMBER() OVER (PARTITION BY DocumentID ORDER BY DateCreated DESC) AS rn
   FROM DocumentStatusLogs
)
SELECT *
FROM cte
WHERE rn = 1</code>
Copy after login
Another method is to use

function instead of cte, especially when each DocumentID has multiple entries in a specific date.

It will ensure that all entries in a specific date get the same ranking. If necessary, you allow you to retrieve two entries in that date.

Conclusion

By using the above technologies, you can effectively extract the latest entries of each group to use sub -query CTE or use the DENSE_RANK function. The method you choose depends on your data requirements and performance considerations. ROW_NUMBER

The above is the detailed content of How can I efficiently retrieve the latest entry for each group in a database table?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template