Skip to content
Advertisement

Clean up ‘duplicate’ data while preserving most recent entry

I want to display each crew member, basic info, and the most recent start date from their contracts. With my basic query, it returns a row for each contract, duplicating the basic info with a distinct start and end date. I only need one row per person, with the latest start date (or null if they have never yet had a start date).

I have limited understanding of group by and partition functions. Queries I have reverse engineered for similar date use partition and create temp tables where they select from. Ultimately I could reuse that but it seems more convoluted than what we need.

I only need to show one row per column. The first 5 columns will be the same always. The last columns depend on contract, and we just need data from the most recent one.

Advertisement

Answer

Change your query to a SELECT DISTINCT on the main query and use a sub-select for DebarkDate column:

(SELECT TOP 1 A.DATEFROM FROM PWORGVESACT A WHERE A.numorgid = ACT.numorgid ORDER BY A.DATEFROM DESC) AS DebarkDate

You can do whatever conversions on the date you need to from the result of that sub-query.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement