SQL Query to Return the Most Recent Row per Client
Question details
The user needs an SQL query to retrieve a single row for each client that represents the most recent record based on a specific date or period column.

- Product
- Database / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Extracting the latest transaction or status update for individual clients from a large database table to generate up-to-date reports.
- Observed behavior
- The goal is to filter a dataset so that only the row containing the maximum PeriodEnd value is returned for every unique ClientId.
Ensure you have access to your database management tool to execute SQL queries, or have your raw dataset exported as a CSV file to process the records directly in a spreadsheet.
Use a Grouped Subquery to Find the Maximum Date
This method utilizes an INNER JOIN combined with a subquery that groups records by client ID to identify the maximum date.
By grouping the data by the unique client identifier and selecting the MAX date, you can isolate the exact latest period for each client. Joining this result back to the original table allows you to retrieve all other column data associated with that specific row.
Determine the name of your table and the relevant columns. For example, use 'AccountsDataTableWithClient' with columns 'ClientId', 'PeriodEnd', and 'Sales'.
Create a subquery that groups by the client identifier: SELECT ClientId, MAX(PeriodEnd) AS MaxPeriodEnd FROM AccountsDataTableWithClient GROUP BY ClientId.
Use an INNER JOIN to connect the main table (a) to the subquery (b) on both the client ID and the date: INNER JOIN (...) AS b ON a.ClientId = b.ClientId AND a.PeriodEnd = b.MaxPeriodEnd.
Run the combined query: SELECT a.ClientId, a.PeriodEnd, a.Sales FROM AccountsDataTableWithClient AS a INNER JOIN (SELECT ClientId, MAX(PeriodEnd) AS MaxPeriodEnd FROM AccountsDataTableWithClient GROUP BY ClientId) AS b ON a.ClientId = b.ClientId AND a.PeriodEnd = b.MaxPeriodEnd;

Use the ROW_NUMBER() Window Function
A modern SQL approach using window functions to assign a sequential number to rows based on the date in descending order.
How to Find the Most Recent Row per Client in WPS Spreadsheet
If you are not familiar with SQL or prefer visual data manipulation, you can export your database table to a CSV format and easily filter for the most recent client records using the built-in sorting and deduplication tools in WPS Spreadsheet.
- 1. Open exported data in WPS: Launch WPS Spreadsheet and open your exported SQL dataset (usually in CSV or XLSX format).
- 2. Sort the dataset: Select your entire data range. Navigate to the 'Data' tab on the top ribbon, click 'Sort', and sort by 'ClientId' (A to Z) as the primary key, and 'PeriodEnd' (Newest to Oldest) as the secondary key.
- 3. Access the Remove Duplicates tool: With the data still selected, stay on the 'Data' tab and click on the 'Remove Duplicates' button.
- 4. Filter the latest records: In the dialog box, uncheck all columns except for 'ClientId' and click 'OK'. Because the data is sorted newest to oldest, WPS Spreadsheet will keep only the first (most recent) row for each client and delete the older duplicates.

Frequently Asked Questions
How do I pull my SQL data into WPS Spreadsheet for analysis?
You can export your SQL query results or raw tables as a CSV file from your database management client. Once exported, simply right-click the CSV file and choose to open it with WPS Spreadsheet, or use the 'Import Data' feature located under the 'Data' tab.
Why does my SQL query return multiple rows for the same client?
This happens if a client has multiple records that share the exact same maximum PeriodEnd date. To fix this, you must introduce a tie-breaker in your query, such as an auto-incrementing ID column or a timestamp, to ensure only one specific row is selected.
Can I use the MAX() function without grouping?
No. When using aggregate functions like MAX() alongside standard columns like ClientId in a SELECT statement, SQL requires you to include a GROUP BY clause for all non-aggregated columns to calculate the result properly.




