Wednesday, 3 May 2017

Sequelize complains "misuse of aggregate: SUM()" but query runs in SQLite

When I run this query in DB Browser for SQLite it returns what I'm after:

SELECT week
, SUM (CASE when activityType = 'rockclimbing' then elapsedTime else 0 END) AS rockclimbing
, SUM (CASE when activityType = 'hike' then elapsedTime else 0 END) AS hike
, SUM (CASE when activityType = 'run' then elapsedTime else 0 END) AS run
FROM activity
WHERE year=2016
AND activityType IN ('rockclimbing', 'hike', 'run')
GROUP BY week
HAVING week < 53
ORDER BY week;

However, when I pass this query to Sequelize using .query() I get the error: misuse of aggregate: SUM()

Here is my Sequelize code:

sequelize.query('SELECT week, activityType, SUM (CASE when activityType = "rockclimbing" then elapsedTime else 0 END) AS rockclimbing, SUM (CASE when activityType = "hike" then elapsedTime else 0 END) AS hike, SUM (CASE when activityType = "run" then elapsedTime else 0 END) AS run FROM activity WHERE year=2016 AND activityType IN ("rockclimbing", "hike", "run") GROUP BY week HAVING week < 53;', { model: Activity }).then(...etc..).catch(...etc...);

What is the problem here? Why does this query work in DB Browser but not in my app?



via jonathanbell

No comments:

Post a Comment