0

Say I have some cats:

interface Cat {
  name: string
  age: number
  color: string
}

I want to sum the ages of cats of each color. This works as expected in (Ala)SQL:

const sql = `
  SELECT name, sum(age)
  FROM ?
  GROUP BY color
`
alasql(sql, [cats])

But if I write a user-defined sum instead:

import {sum} from 'lodash'
alasql.fn.mySum = function(xs) { sum(xs) }

const sql = `
  SELECT name, mySum(age)
  FROM ?
  GROUP BY color
`
alasql(sql, [cats])

then mySum is called with xs equal to undefined. What am I doing wrong here?

Sasgorilla
  • 2,403
  • 2
  • 29
  • 56

1 Answers1

0

I am not sure if this is exactly what you are looking for but I think it might look something like:

import {sum} from 'lodash'
alasql.fn.mySum = function(xs) { return sum(xs) }
alasql(SELECT name, mySum(age) FROM ? GROUP BY color, [cats])

Maybe you forgot the return keyword in your mySum function.

deleuterio
  • 548
  • 1
  • 4
  • 17
bab245
  • 11
  • 1
  • Alasql is a Javascript library running in the browser -- I've added the `[Javascript]` tag to help clarify that. (The final line of this answer is not valid Javascript.) – Sasgorilla May 19 '23 at 00:55