Skip to content
Advertisement

Find number of rows with each property value, taking into account only the most recent rows in SQL

I have a database with tables that represents “edits” to “pages”. Every edit has an ID and a timestamp and a “status”, which has certain discrete values. Pages have IDs and also have “categories”.

I wish to find the number of pages with each status within a given category, taking into account only the state as of the most recent edit.

Edits:

Pages:

I want to get, for category 100:

Page 10 and 30 have two edits, but the later one “overrides” the first one, so only the edits with status 91 and 92 are counted. Pages 20 and 40 account for one of 91 and 90 each and page 50 is in the wrong category so it doesn’t feature.

I have tried the following, but it doesn’t seem to work. The idea was to select the max (i.e. latest) edit for each page with the right category. Then join that to the edit table and group by the status and count the rows:

For example in this fiddle: http://sqlfiddle.com/#!9/42f2ed/1

The result is:

What is the correct way to get this information?

Advertisement

Answer

Output:

cat_id stat cnt
100 90 1
100 91 2
100 92 1
200 90 1

https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=7592c7853481f6b5a9626c8d111c1d3b (the query is applicable to MariaDB 10.1).


Is it possible to join on the edit_id (which is unique key for each edit)? – Inductiveload

No, this is impossible. cnt=2 counts two different edit_id values – what value must be used?

But you may obtain concatenated values list – simply add GROUP_CONCAT(edit_id) into the output list.

https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=b2391972c3f7c4be4254e47514d0f1da

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