I have a table describing files with a datetime field. I want to somehow create a report that gives me the number of files grouped by each year, number of files grouped by each year and month and number of files grouped by each year, month and day. I just want records where count(*) > 0. Preferably using the ORM in django or if that`s not possible, using some SQL that runs on both PostgreSQL and SQLite.
The number of records in this database can be huge so my attempts to do this in code, not in SQL ( or indirectly in SQL thru ORM ) don’t work and if I get it to work I don`t think it will scale at all.
Grateful for any hints or solutions.
Normally I work on Oracle but a quick google search showed that this should also work for Postgres. For the minutes you could do like this
That works then all the way down to years:
You are only getting the years where you got something. I think that is what you wanted.
Edit: You need to build an index on “yourtimestamp” otherwise the performance is ugly if you do have a lot of rows.