I'm trying to write a query that will return the entry with the highest count for each day across a range of BigQuery tables.
I've only been able to get as far as writing the following query, which returns all of entries and their counts for each day, sorted by day and then by the products with the highest entries from highest to lowest.
SELECT
STRFTIME_UTC_USEC(UTC_USEC_TO_day((ts-25200000)*1000),"%Y-%m-%d") AS day,
products.id as product,
count(products.id) as num_entries
FROM
TABLE_DATE_RANGE([table_name_], timestamp('20170801'), timestamp(current_date()))
GROUP BY day, product
ORDER BY day, num_entries desc
e.g.
2017-08-01 . product A . 10
2017-08-01 . product B . 8
2017-08-01 . product C . 4
2017-08-01 . product D . 2
2017-08-02 . product X . 18
2017-08-02 . product Y . 15
2017-08-02 . product Z . 11
2017-08-03 . product N . 20
2017-08-03 . product M . 12
2017-08-03 . product N . 5
2017-08-03 . product O . 3
...
How could I change the query to return only the top entry (highest num_entries) for each day?
e.g.
2017-08-01 . product A . 10
2017-08-02 . product X . 18
2017-08-03 . product N . 20
...