You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found that joins are not working(not giving the desired result). Please follow below steps to recreate problem.
//Create table one
CREATETABLEone (a INT, b INT);
INSERT INTO one VALUES(1,10),(2,20);
//Create Table two
CREATETABLEtwo (a INT, c INT);
INSERT INTO two VALUES(1,100),(2,200);
Now when I run the following query
SELECT t0.`a`AS a, c -b AS bc FROM (SELECT a,b FROM one) t0 JOIN (SELECT a,c FROM two) t1 ON t0.`a`=t1.`a`;
Result is:
-----------
# a c
1 1 100
2 2 200
But the expected result is(if u run in any other relational db)
changed the title [-]Incorrect result when combining JOIN with select alias on calculated value[/-][+]Incorrect result when JOINing a subselect[/+]on Feb 20, 2017
For now you can get by if you put the result from your subselects into a (temporary) table (or in this example use the name two instead of the subselect) you can also put your subselect into a parameter (as shown in the jsfiddle code)
Any inputs from @agershun why a join on a subselect will reset the list of column names in use?
@mathiasrw Thank you for your very quick reply and work around suggested.
Could you please let us know when we can expect the support for joining the subselect. Just curious, as it is highly required for one of our current project.
I haven't worked on this in a few month (I'll be getting back to it soon) but right now you have to materialize each of the sub-query result sets and pass them in using question marks in the final statement.
varsub=alasql("SELECT * FROM my_sub_table WHERE...");varsub2=alasql("SELECT * FROM my_sub_table2 WHERE...");varfinal=alasql("SELECT * FROM my_table ? ON (...) JOIN ? ON (...) WHERE ...",[sub,sub2]);
This works and is pretty fast since it's all in memory and whatnot.
I actually got the JOIN working with a WITH clause!
WITH
foo AS (
SELECT geo, SUM(num) AS num
FROM table1
WHERE
type ="B"GROUP BY geo
),
bar AS (
SELECT geo, SUM(num) AS num
FROM table1
WHERE
type ="A"GROUP BY geo
)
SELECTfoo.geoAS aggregation,
foo.num/bar.numAS val
FROM foo
JOIN bar
ONfoo.geo=bar.geo;
Thanks for your approach though, it can be very useful to reuse the temp tables.
The strange thing is that using the (unsupported) sub-SELECT there is no error given, just the result of the last sub-SELECT. Is that intended?
Glad you found it useful!
Let me know if a documentation PR can be useful to include this info in the README, and where you think it might be inserted.
is there any way to make this work using JS templates strings? I'm getting "cannot read property 'team' of undefined" - or is there a better way to do this?
const x = alasql(
`
WITH
homeScore AS (
SELECT team, SUM(homeScore) as homePoints FROM boys WHERE homeAway = "H" GROUP BY team
),
awayScore AS (
SELECT team, SUM(awayScore) as awayPoints FROM boys WHERE homeAway = "A" GROUP BY team
)
SELECT *
FROM boys
JOIN homeScore
ON awayScore.team = homeScore.team
`
);
edit: I was able to use this syntax
const x = alasql(
`WITH
homeScore AS (
SELECT team, SUM(homeScore) as homePoints FROM boys WHERE homeAway = 'H' GROUP BY team
),
awayScore AS (
SELECT team, SUM(awayScore) as awayPoints FROM boys WHERE homeAway = 'A' GROUP BY team
)
SELECT *
FROM homeScore
JOIN awayScore
USING team
`
);
For now you can get by if you put the result from your subselects into a (temporary) table (or in this example use the name two instead of the subselect) you can also put your subselect into a parameter (as shown in the jsfiddle code)
Any inputs from @agershun why a join on a subselect will reset the list of column names in use?
The right question here would be why the main query returns the subselect results. And bobviously this is because the join is not finished properly! This is relevant otherwise more confusion is brought instead as this is not related to columns... at this fisrt stage.
Once solved there is another issue (second stage): the subselect support in joins is incomplete (in fact, there is no code to resolve defColumns and it takes a default resolution which is wrong to get the right data from the scope).
Therefore, to resolve this issue = bug fix in joins + add missing code to support the use case.
Activity
mathiasrw commentedon Feb 20, 2017
Thank you for a very well described bug.
We might need @agershun to give a hint to where the but could be located?
[-]Joins are not working[/-][+]Incorrect result when combining JOIN with select alias on calculated value[/+][-]Incorrect result when combining JOIN with select alias on calculated value[/-][+]Incorrect result when JOINing a subselect[/+]mathiasrw commentedon Feb 20, 2017
The problem is in joining a subselect.
Demonstration of the problem: http://jsfiddle.net/L11cnfbu/
For now you can get by if you put the result from your subselects into a (temporary) table (or in this example use the name
twoinstead of the subselect) you can also put your subselect into a parameter (as shown in the jsfiddle code)Any inputs from @agershun why a join on a subselect will reset the list of column names in use?
aravinds502 commentedon Feb 21, 2017
@mathiasrw Thank you for your very quick reply and work around suggested.
Could you please let us know when we can expect the support for joining the subselect. Just curious, as it is highly required for one of our current project.
mathiasrw commentedon Feb 21, 2017
All code here is added when people feel like contributing. There is no timeline.
As soon as someone provides a PR with a fix I will release a new version.
Another alternative is to pay to get the fix implemented. Let me know if that has any interest to you.
sasha-alias commentedon Mar 12, 2017
not sure, is this the part of the code related to the issue?
looks like some implementation started but commented out, most probably not finished.
mathiasrw commentedon Mar 12, 2017
Hmmmmm. Interesting...
besquared commentedon Aug 31, 2017
+1 just ran into this today.
caesarsol commentedon Mar 30, 2018
Hi, a solution for this would be very appreciated!
In the meanwhile, could you describe what is the most straight-ahead workaround?
besquared commentedon Mar 30, 2018
I haven't worked on this in a few month (I'll be getting back to it soon) but right now you have to materialize each of the sub-query result sets and pass them in using question marks in the final statement.
This works and is pretty fast since it's all in memory and whatnot.
caesarsol commentedon Mar 30, 2018
I actually got the
JOINworking with aWITHclause!Thanks for your approach though, it can be very useful to reuse the temp tables.
The strange thing is that using the (unsupported) sub-SELECT there is no error given, just the result of the last sub-SELECT. Is that intended?
mathiasrw commentedon Apr 2, 2018
thank you for a great input @caesarsol
caesarsol commentedon Apr 10, 2018
Glad you found it useful!
Let me know if a documentation PR can be useful to include this info in the README, and where you think it might be inserted.
mathiasrw commentedon Apr 11, 2018
That would be awesome!
randomlysa commentedon Jan 17, 2019
is there any way to make this work using JS templates strings? I'm getting "cannot read property 'team' of undefined" - or is there a better way to do this?
edit: I was able to use this syntax
pabloBelt commentedon Feb 12, 2023
The right question here would be why the main query returns the subselect results. And bobviously this is because the join is not finished properly! This is relevant otherwise more confusion is brought instead as this is not related to columns... at this fisrt stage.
Once solved there is another issue (second stage): the subselect support in joins is incomplete (in fact, there is no code to resolve defColumns and it takes a default resolution which is wrong to get the right data from the scope).
Therefore, to resolve this issue = bug fix in joins + add missing code to support the use case.
Fix joined subqueries in expressions to close #832 (#2579)
mathiasrw commentedon Oct 21, 2026
Fix released as part of v4.21.0