Repository navigation
Closed
Description
I'm trying to use UNION ALL for selecting two different columns of sometimes the same rows in order to create a list of dates, see below a simplified version of what I'm trying to do:
alasql('SELECT [projectID], [Date1] FROM ? UNION ALL SELECT [projectID], [Date2] FROM ?',[projects])As far as I know, this should work. However, no matter what I do, as long as UNION or UNION ALL is present in the code I get the following error:
alasql.js:6580 Uncaught Error: Data source number 0 in undefined
doJoin @ alasql.js:6580
queryfn3 @ alasql.js:6086
queryfn2 @ alasql.js:6061
(anonymous function) @ VM3835:3
(anonymous function) @ alasql.js:6012
queryfn @ alasql.js:6009
tatement @ alasql.js:7031
queryfn3 @ alasql.js:6172
queryfn2 @ alasql.js:6061
(anonymous function) @ VM3833:3
(anonymous function) @ alasql.js:6012
queryfn @ alasql.js:6009
statement @ alasql.js:7031
alasql.dexec @ alasql.js:4304
alasql.exec @ alasql.js:4255
alasql @ alasql.js:128
(anonymous function) @ functions.js:1635 => this is where I have the above code
(anonymous function) @ alasql.js:7044
queryfn3 @ alasql.js:6330
queryfn2 @ alasql.js:6061
(anonymous function) @ alasql.js:15353
xhr.onload @ alasql.js:3443
(I used the latest alasql.js 0.2.6 version.)
Am I doing something wrong, or is this a bug?
Reacted by Copilot
Activity
mathiasrw commentedon May 3, 2016
Thank you for a really well described error.
Does @agershun have any inputs on this?
tmrk commentedon May 11, 2016
Any news on this?
For now, I am using the following method to chain my results like this:
But I have a feeling that a functioning
UNIONwould be faster (not to mention cleaner):... especially because it would require only one alasql call to the database.
Anyway, I just thought I'd share, maybe others might have the same problem for now.
mathiasrw commentedon May 11, 2016
just saw your original sql
first
?will give the first index (projects) and second?will give the second index - but there is no data in the second index.Please try again with something like
or
Depending on what style you prefer...
tmrk commentedon May 11, 2016
You sir are absolutely right.
However, one issue still remains:
ORDER BYdoes not seem to have an effect on the joined results. If I take my code from above:The result is just like
ORDER BYwasn't there. I tried addingASCorDESC, no change.Any ideas?
tmrk commentedon May 11, 2016
I made a jsfiddle to demonstrate
ORDER BYfailing: https://jsfiddle.net/gf845cfp/[-]UNION is not working[/-][+]UNION ignores ORDER BY[/+]mathiasrw commentedon May 12, 2016
As you have no
WHEREstatement the order by semanticly belongs to the second select of the union all.I tried to put a
(...)around, but the parser can not handle it.So - the solution for you is to add
WHERE 1before your ORDER BYhttps://jsfiddle.net/gf845cfp/1/
UPDATE: I need new glasses...
9 remaining items
mathiasrw commentedon May 26, 2016
Hi @tmrk
Had a look with fresh eyes. I dont know how I could miss this. The problem is that its sorting on an alias - and for some reason its not working for UNION
The solution is th have the union as a subquery - not both results have same sorting:
https://jsfiddle.net/hu0hzsrn/
tmrk commentedon May 26, 2016
Thank you! I confirm that this is working.
Sorting on alias shouldn't be a problem otherwise, as it's valid SQL.
(See the same example here: http://sqlfiddle.com/#!9/3983f/1 - maybe an idea for future SQL compliance?)
Anyway, thanks Mathias!
mathiasrw commentedon May 27, 2016
Glad I could help.
True. I will make an "fresh" issue on the matter.
mathiasrw commentedon May 27, 2016
Hmm. Looks like it works in when there is no sub or union
https://jsfiddle.net/0dozpgvq/
I will reopen this to make sure I remember to identify the problem.
Add test for UNION with ORDER BY (issue #671)
Add test for UNION with ORDER BY to confirm #671 (#2277)