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
{{ message }}
Repository navigation
GROUP BY returns 1 invalid result if no rows meet condition #79
Another issue with GROUP BY is that any field in the result that is not in the group is 'undefined'. MySQL simply returns the data from the first retrieved row.
alasql('SELECT Type,Name FROM ? GROUP BY Type',[[{Type:'PT',Name:'Steve'},{Type:'PT',Name:'Dan'},{Type:'FT',Name:'John'}]]);
Yes, for first question, I am able to get away with this as I already always check for results:
var res=alasql('SELECT pkey,Name FROM ? WHERE 1=0 GROUP BY Name',[[{Name:'test'}]]));
if(res.length>0 && typeof res[0].pkey !== 'undefined'){} //use a field that is guaranteed to never be undefined
Sorry, just a tweak on your example for others. (It works exactly as I would have expected it to from within mySQL)
var res = alasql('SELECT Type,FIRST(Name) FROM ? GROUP BY Type',[...]);
returns [[{Type:"foo","FIRST(Name)":"bar"}]]
Make sure you include the as statement.
var res = alasql('SELECT Type,FIRST(Name) AS Name FROM ? GROUP BY Type',[...]);
will return [[{Type:"foo",Name:"bar"}]]
Is there a way you can test if the fields are requested naked, to automatically apply the FROM() function when used in a GROUP BY? Maybe in an upcoming version?
Is there a way you can test if the fields are requested naked, to automatically apply the FROM() function when used in a GROUP BY?
I think, that the standard SQL reaction is to throw a error like "column is not found". I think, Alasql can check this, but only for SELECT with GROUP BY clause. Plus, it is hard to recognize these "naked" columns in expressions, like:
Activity
silverhawk184 commentedon Mar 9, 2015
Another issue with GROUP BY is that any field in the result that is not in the group is 'undefined'. MySQL simply returns the data from the first retrieved row.
Returns
Should be
agershun commentedon Mar 10, 2015
Thank you! For the second question you should:
a) add Name to the GROUP BY clause
b) Use FIRST() aggregator
or
For the first question - sorry, it still in progress...
agershun commentedon Mar 10, 2015
Try this example in jsFiddle
silverhawk184 commentedon Mar 10, 2015
Yes, for first question, I am able to get away with this as I already always check for results:
silverhawk184 commentedon Mar 10, 2015
Sorry, just a tweak on your example for others. (It works exactly as I would have expected it to from within mySQL)
returns [[{Type:"foo","FIRST(Name)":"bar"}]]
Make sure you include the as statement.
will return [[{Type:"foo",Name:"bar"}]]
Is there a way you can test if the fields are requested naked, to automatically apply the FROM() function when used in a GROUP BY? Maybe in an upcoming version?
mathiasrw commentedon Mar 24, 2015
👍
agershun commentedon Mar 24, 2015
Is there a way you can test if the fields are requested naked, to automatically apply the FROM() function when used in a GROUP BY?
I think, that the standard SQL reaction is to throw a error like "column is not found". I think, Alasql can check this, but only for SELECT with GROUP BY clause. Plus, it is hard to recognize these "naked" columns in expressions, like:
mathiasrw commentedon Jul 14, 2015
Is this issue resolved?
dogma-systems commentedon Jun 10, 2018
There is news about this bug? Thanks in advance
mathiasrw commentedon Jun 11, 2018
Well... its now returning
[{}]instead of the original problem - but its still not correct.Any help to resolve this would be awesome.
http://jsfiddle.net/t780pnz1/
tran4o commentedon Feb 17, 2020
adding HAVING count(*) > 0 seems to fix this problem
Test for GROUP BY with no matching rows to confirm #79 is fixed (#2300)
mathiasrw commentedon Dec 6, 2025
Fix released in v4.11.0