My table stores various versions of the few documents.
-------------------------------
| id | doc_type | download |
-------------------------------
| 1 | type1 | file |
-------------------------------
| 2 | type2 | file |
-------------------------------
| 3 | type3 | file |
-------------------------------
| 4 | type1 | file |
-------------------------------
The table stores different versions of the same type of documents. I need to build a query which will return distinct types of doc_type having max(id) - which is the newest version of the file. Number of doc_types is not limited and is dynamic. My query so far:
select max(id) from template_table
where doc_type in (select distinct doct_type from template_table);
This returns only one largest result. If I could sort results by id ASC and the limit result to 4 largest but it will not guarantee that it will return distinct doc_types. Also number of document types in DB might be changing from 4 it needs to count how many there is.
select * from template_table
order by id limit 4;
Thanks for any help.