0

I am trying to create an aggregate query of Gravity Forms submissions. My client has 8 different forms, each with 5 questions.

A user will fill out the same forms multiple times over the course of time.

My challenge is to to only query the latest submissions for each form_id and the underlying data for a given user.

So far, I have this:

 SELECT e.id as entry_id, e.form_id as form_id, e.date_created as date_created, m.meta_key as meta_key, m.meta_value as meta_value
FROM wplh_gf_entry e
JOIN wplh_gf_entry_meta m on e.id = m.entry_id
WHERE e.created_by = 5 AND meta_key BETWEEN 1 AND 5 ( meta_key in this case hold the id of a given question for each form)

However, the query above does nothing to limit the results to only the latest entries per form_id. If I put a max(e.date_created) and GROUP BY e.form_id then I only get the first entry for each form out of the meta table.

The relevant fields in each table are as follows: wplh_gf_entry (id, form_id, date_created, created_by) wplh_gf_entry_meta(form_id, entry_id, meta_key, meta_vaue)

I'm not quite sure what I need here. I tried a few different subqueries as well but couldn't get any workable results.

Thank you in advance.

Difster
  • 3,264
  • 2
  • 22
  • 32
  • 1
    Please see https://meta.stackoverflow.com/questions/333952/why-should-i-provide-a-minimal-reproducible-example-for-a-very-simple-sql-query – Strawberry Aug 25 '20 at 06:29
  • The answers to the first duplicate explain how to get the top record per group. The second duplicate shows how to join an additional table on the top records per group resultset. – Shadow Aug 25 '20 at 06:50

0 Answers0