_(์ฐธ๊ณ : ์ ๋ Postgres๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ์ด๊ฒ์ ๋ค๋ฅธ ๋ฐฉ์ธ์๋ ์ ์ฉ๋์ง ์์ ์ ์์ต๋๋ค)_
ํ๊ท ์ ์์ ํด์ฆ๋ฅผ ํต๊ณผํ ์ฌ๋์ ์๋ฅผ ๋ฐํํ๋ ์ฟผ๋ฆฌ๊ฐ ์์ต๋๋ค. ๋ค์์ ๋ด๊ฐํ๋ ค๋ ์์ ์ ๋จ์ํ ๋ ๋ฒ์ ์ ๋๋ค.
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' );
( sum
์ฐธ๊ณ )
๋ถํํ๋ ์์๋๋ก SUM
๊ธฐ๋ฅ์ ๋ ๋๋งํ์ง ์์ต๋๋ค.
sum("(case when qs"."score >= 60 then 1 else 0 end)") as "passedCount"
sumRaw
๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ ๊ฒ์ ๋ฐ๋ํ๋ ์ฌ๋์ด ์์ต๋๊น?knex.raw
๋ก sum
์ ์ธ์๋ฅผ ๋ํํ๋ฉด ์๋ํฉ๋๊น?
๊ทธ๋ ์ง ์์ ๊ฒ ๊ฐ์ต๋๋ค ... ํฉ๊ณ๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํ๋ฉด :
.sum( knex.raw( '(case when qs.score >= 60 then 1 else 0 end) AS passedCount' ) )
"val.toLowerCase๊ฐ ํจ์๊ฐ ์๋๋๋ค" ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
( as
๋ณ์นญ์ ์ถ๊ฐํ์ง ์๊ณ ๋นผ์ ๊ดํธ๋ก ์๋ํ์ง๋ง ์ค๋ฅ๋ ๋งค๋ฒ ๋์ผํ์ต๋๋ค)
์คํ ์ถ์ :
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 ๋ฒ์ ์ 0.11.7์ด์ง๋ง 0.11.9๋ก ์ ๊ทธ๋ ์ด๋ํ๋๋ฐ ๋์ผํ ๋ฉ์์ง๊ฐ ๋ํ๋ฉ๋๋ค.
#238์์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ฐพ์์ต๋๋ค. $#$ sum
select
์์ knex.raw
๋ฅผ ์ํํ์ญ์์ค. ์ง๊ธ ์ฐ๋ค๋ณด๋ ๋ปํ๋ฏ...
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' );
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
#238์์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ฐพ์์ต๋๋ค. $#$
sum
select
์์knex.raw
๋ฅผ ์ํํ์ญ์์ค. ์ง๊ธ ์ฐ๋ค๋ณด๋ ๋ปํ๋ฏ...