0

I have a MySQL table, an excerpt of which is here:

http://pastebin.com/SdYJbzgk

I need to keep the most recent row for each row where the assoc_case, document, and participant are the same. For example, I only want to keep row 136 out of rows 133-136.

I'm working from this, but can't seem to adapt it for my needs:

SELECT
    id,
    assoc_case,
    participant,
    document,
    MAX(created)
FROM
    `table`
GROUP BY
    created,
    assoc_case,
    participant,
    document
Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58
Tyler Shuster
  • 437
  • 1
  • 5
  • 12

1 Answers1

1

In MySQL, you do this using join:

delete t
    from table t join
         (select assoc_case, participant, document, max(created) as maxc
          from table
          group by assoc_case, participant, document
         ) tt
         on t.assoc_case = tt.assoc_case and t.participant = tt.participant and
            t.document = tt.document
    where t.created < tt.maxc;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786