Note that there are some explanatory texts on larger screens.

plurals
  1. POT-SQL - Filtering records based on a date
    text
    copied!<p>I am writing SQL Server queries and need a solution for how to filter the rows that are returned properly.</p> <p>The basic setup is as follows - I am selecting a bunch of records from a table based primarily on an identifier. So, for a given identifier there might be 100 records that are returned initially. Within these 100 records, however, there are a number of them that need to be deleted - not because they are duplicates, but because one is newer than the other. So I essentially just need a way to further filter the results based on whichever record was created/modified most recently.</p> <p>I know that ideally, these "old" records should not be in the database, but I don't have control over that. What is happening is essentially people are updating the entries over time to reflect new information, but rather than editing the "old" entry, a new one gets entered each time. Thus, there might be 3 entries for a given identifier, but I only need the one that was most recently entered.</p> <p>Is there an easy way to do this in the T-SQL query string? It would be similar to the "Last of" function in Access queries. I do have a properly formatted date column for each record.</p> <p>Thanks!</p> <p>My query string so far (excuse the VB syntax):</p> <pre><code> "SELECT *" &amp; _ "FROM Performance_Override " &amp; _ "WHERE ([Deal_Name] = " &amp; "'" &amp; Range("ID").value &amp; "'" &amp; " or" &amp; _ " [UNIQUE_ID] LIKE " &amp; "'" &amp; "%SPLIT_LOAN%" &amp; "')" &amp; " AND" &amp; _ " ([Scenario] = " &amp; "'" &amp; "BASE" &amp; "')" &amp; _ "ORDER BY [Date] ASC; " </code></pre>
 

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