I'm building a program that uses Bookshelf to access a MySQL database. One of the things I'm trying to make is grouping data by two columns. But I can't figure out how to do it.
I tried using knex groupByRaw and lodash _.groupBy, but I don't think I'm using them in the right place.
Here's the situation:
Table
I'm storing timers in a table with the following columns: id, userID, projectID, startTime, stopTime.
I want to select the acumulative times for each user in a given calendar week.
My guess is the resulting query would be something like this:
SELECT SUM(TIMESTAMPDIFF(SECOND, startTime, endTime)), userID, YEARWEEK(startTime, 1), projectID
FROM time
GROUP BY userID, YEARWEEK(startTime, 1)
HAVING projectID = 1