mercredi 24 février 2016

SQL select by column of newest element of has_many relationship

In my project I have a tasks(id, name) table. I also have a status_updates(id, task_id, user_id, status, created_at) table.

I want to select tasks whose newest status_update equals a certain status.

I cannot figure out how to combine the following things, however:

  1. Only use the newest status_update for each task.
  2. Filter away the status_updates that do not have the correct status.
  3. Wrap this in a INNER JOIN so the final result is a collection of tasks.

When using GROUP_BY, it seems that 1. and 2. cancel each other out. An example:

SELECT * FROM status_updates GROUP_BY task_id ORDER created_at DESC

gives back only the newest status_update for each task. However, this:

SELECT * FROM status_updates WHERE status='1' GROUP_BY task_id ORDER created_at DESC

executes the WHERE before the ORDER, thus giving back from the ones with a correct status, the newest, instead of from the newest, only those with a correct status.

I also tried using HAVING, but as it only works on aggregate columns I could not figure out how to use it properly, or if it was at all helpful in this scenario.

I am using Ruby on Rails, so I'd like an answer to work both on MySQL as well as SQLite.

Aucun commentaire:

Enregistrer un commentaire