Knex: How to perform complex Sums

Created on 4 Aug 2016  ·  3Comments  ·  Source: knex/knex

_(Note: I'm using Postgres; this may not apply to other dialects)_

I have a query that returns an average score, and the number of people that have passed, a quiz. Here is a simplified version of what I'm trying to do:

knex( 'quiz_submissions AS qs' )
        .select( 'q.name AS quiz_name' )
        .avg( 'qs.score' )
        .sum( '(case when qs.score >= 60 then 1 else 0 end) AS passedCount' )
        .innerJoin( 'quizzes AS q', 'q.id', 'qs.quiz_id' )
        .groupBy( 'q.name' );

(Note the sum)

Unfortunately, that doesn't render the SUM function as I expect it:

sum("(case when qs"."score >= 60 then 1 else 0 end)") as "passedCount"
  1. Is there a way to do "complex" sums currently?
  2. If not, is anyone opposed to me adding a sumRaw function?

Most helpful comment

Found a workaround in #238; Do the knex.raw in the select rather than the sum. Seems obvious now that I type it out...

knex( 'quiz_submissions AS qs' )
    .select( 'q.name AS quiz_name', knex.raw( 'SUM(case when qs.score >= 60 then 1 else 0 end) AS passedCount' ) )
    .avg( 'qs.score' )
    .innerJoin( 'quizzes AS q', 'q.id', 'qs.quiz_id' )
    .groupBy( 'q.name' );

All 3 comments

Does it work if you wrap your argument to sum with knex.raw?

Doesn't look like it... If I change the sum to this:

.sum( knex.raw( '(case when qs.score >= 60 then 1 else 0 end) AS passedCount' ) )

I get the error "val.toLowerCase is not a function"

(I tried with parenteses, without, adding the as alias, taking it away, the error was the same each time)

Stack trace:

TypeError: val.toLowerCase is not a function
    at QueryCompiler_PG.aggregate (../../src/query/compiler.js:166:25)
    at QueryCompiler_PG.columns (../../src/query/compiler.js:152:25)
    at ../../src/query/compiler.js:82:22
    at Array.map (native)
    at QueryCompiler_PG.select (../../src/query/compiler.js:81:35)
    at QueryCompiler_PG.toSQL (../../src/query/compiler.js:45:5)
    at QueryBuilder.toSQL (../../src/query/builder.js:41:49)
    at ../src/runner.js:34:7
    at tryCatcher (util.js:16:23)
    at using.js:185:26
    at tryCatcher (util.js:16:23)
    at Promise._settlePromiseFromHandler (promise.js:504:31)
    at Promise._settlePromise (promise.js:561:18)
    at Promise._settlePromise0 (promise.js:606:10)
    at Promise._settlePromises (promise.js:685:18)
    at Promise._fulfill (promise.js:630:18)
From previous event:
    at Promise.longStackTracesCaptureStackTrace [as _captureStackTrace] (debuggability.js:369:19)
    at Promise._then (promise.js:230:17)
    at Promise.then (promise.js:123:17)
    at Function.Promise.using (using.js:169:14)
    at Runner.run (../src/runner.js:27:20)
    at QueryBuilder.Target.then (../src/interface.js:22:50)
    at process._tickCallback (node.js:369:9)

knex version is 0.11.7, although I upgraded to 0.11.9 and got the same message.

Found a workaround in #238; Do the knex.raw in the select rather than the sum. Seems obvious now that I type it out...

knex( 'quiz_submissions AS qs' )
    .select( 'q.name AS quiz_name', knex.raw( 'SUM(case when qs.score >= 60 then 1 else 0 end) AS passedCount' ) )
    .avg( 'qs.score' )
    .innerJoin( 'quizzes AS q', 'q.id', 'qs.quiz_id' )
    .groupBy( 'q.name' );
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mishitpatel picture mishitpatel  ·  3Comments

aj0strow picture aj0strow  ·  3Comments

legomind picture legomind  ·  3Comments

saurabhghewari picture saurabhghewari  ·  3Comments

zettam picture zettam  ·  3Comments