Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Show first record for series of unique identifiers

I have a table that includes the following fields

  • trackingNo
  • event
  • timestamp

I am trying to find the first event, based on timestamp, for a series of tracking numbers. I know I can pull all events for specific tracking numbers using the following.

SELECT trackingNo, timestamp
FROM t
WHERE trackingNo in ("122345", "67890", "24680:)

This would return the following results

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

trackingNo timestamp
12345 2023-04-12T15:36:05
12345 2023-04-12T17:00:00
12345 2023-04-12T19:00:00
67890 2023-04-08T08:00:00
67890 2023-04-08T08:05:00
67890 2023-04-08T08:10:05
24680 2023-04-10T19:00:00
24680 2023-04-10T19:01:00

How do I return just the first record for each tracking number based on timestamp? I want the results to show

trackingNo timestamp
12345 2023-04-12T15:36:05
67890 2023-04-08T08:00:00
24680 2023-04-10T19:00:00

I’ve tried TOP and HAVING MIN but that only returned one result.

>Solution :

Min to get the earliest timestamp and group by to get one for each of the tracking numbers.

  SELECT trackingNo
       , MIN(timestamp) timestamp
    FROM t
   WHERE trackingNo in ("122345", "67890", "24680:)
GROUP BY trackingNo
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading