Skip to content

Can't reference table name in subquery if grouped #1106

Closed

Description

@macrat

I made table like this.

team name score
A alice 10
A alice 5
A bob 5
C charlie 10

And execute this SQL.

SELECT team, name, SUM(score) AS personal_score, (
  SELECT SUM(score)
  FROM test AS i
  WHERE i.team = o.team
) AS group_score
FROM test AS o
GROUP BY name

I want result like this. (And work in sqlite3)

team name personal_score group_score
A alice 15 20
A bob 5 20
B charlie 10 10

but alasql couses this error.

TypeError: Cannot read property 'team' of undefined

If remove GROUP BY, query was work well.

SELECT team, name, (
  SELECT SUM(score)
  FROM test AS i
  WHERE i.team = o.team
) AS group_score
FROM test AS o
team name group_score
A alice 20
A bob 20
B charlie 10

Activity

  1. gregja commented on Apr 23, 2021

    @gregja

    Hi,
    This issue is old (but always open), and I had recently a similar problem than @macrat.
    I found a solution which worked in my case, so I think it should work for @macrat.
    Here is the technique I would use :

    • first I would replace the 2 occurrences of the table "test" by "?"
    SELECT team, name, (
      SELECT SUM(score)
      FROM ? AS i
      WHERE i.team = o.team
    ) AS group_score
    FROM ? AS o
    • second : for the execution of the request, I would pass the same dataset twice
      alasql(query, [dataset, dataset]);

    I hope it will help.
    Best regards

  2. mathiasrw commented on Aug 20, 2026

    @mathiasrw
    Member

    A fix was released as part of v4.18.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions