DEV Community

Cover image for Find the Closest Date Match for Each Record from Two Tables — From SQL to SPL #20
Judith-Data-Processing-Hacks
Judith-Data-Processing-Hacks

Posted on

2 1 1 1 1

Find the Closest Date Match for Each Record from Two Tables — From SQL to SPL #20

Problem description & analysis:

SQL Server has two tables, Table1:

table 1

Table2:

table 2

Task: Now we need to sort Table1 by ID and traverse each record in sequence: retrieve the record in Table2 that has the same DocNum field as the current record, but with a slightly later time (the earliest among all later records). The special rule is that records taken from Table2 cannot be retrieved again next time.

expected results

Code comparisons:

SQL

WITH CTE1 As (
SELECT t1.ID, t1.JoiningDt, t1.DocNum,
  (SELECT TOP 1 ClosestDt FROM Table2 
  WHERE DocNum = t1.DocNum AND ClosestDt > t1.JoiningDt ORDER BY ClosestDt  ASC   ) ClosestDt
FROM Table1 t1
 ), CTE2 AS (
  SELECT
       ID, JoiningDt, DocNum, ClosestDt
    , ROW_NUMBER() OVER(PARTITION BY DocNum,   ClosestDt ORDER BY ID) rn
  FROM CTE1
  )
SELECT ID, JoiningDt, DocNum, 
  CASE WHEN rn = 1 then ClosestDt ELSE
  (SELECT ClosestDt FROM Table2 
  WHERE DocNum = c1.DocNum AND ClosestDt > c1.JoiningDt ORDER BY ClosestDt  ASC   
  OFFSET c1.rn -1  ROWS FETCH NEXT 1 ROWS ONLY) END as ClosestDt
  FROM CTE2 c1
Enter fullscreen mode Exit fullscreen mode

Ordered calculations need to be performed here, especially to implement the rule that record cannot be retrieved again after being taken. SQL needs to create sequence numbers and flag bits, and multiple layers of nesting are used in conjunction with join statements to indirectly implement it. The code is cumbersome and difficult to understand; Using stored procedures would be relatively intuitive, but the code would be longer and the structure would become more complex.

SPL: SPL can directly implement it according to business logic.👉🏻 Try.DEMO

expected results

A1-A2: Load data.

A3: Loop through each record in A1 and add new fields. The business logic is to retrieve the record from A2 with the same DocNum as the current record but later, retrieve the ClosestDt of the first record, and then delete this record from A2 to avoid being retrieved again. Function select@1 means to select the first record that meets the criteria.


Free to Try, Powerful to Use — esProc SPL FREE Download.

Image of Datadog

Optimize UX with Real User Monitoring

Learn how Real User Monitoring (RUM) and Synthetic Testing provide full visibility into web and mobile performance. See best practices in action and discover why Datadog was named a Leader in the 2024 Gartner MQ for Digital Experience Monitoring.

Tap into UX Best Practices

Top comments (1)

Collapse
 
judith677 profile image
Judith-Data-Processing-Hacks

Discuss with the communities!

✅Discord:discord.gg/sxd59A8F2W
✅Reddit:reddit.com/r/esProc_Desktop/

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay