在使用alasql(https://github.com/agershun/alasql)的GROUP BY查询中使用UNION或UNION ALL时,仅检索来自table1的结果。
运行单独的查询会改为输出正确的结果。
请参阅此jfiddle http://jsfiddle.net/L8471bnk/116/
var data= [
{"label":"transport - car","value":800},
{"label":"airplane","value":234},
{"label":"train","value":500},
{"label":"glider","value":123},
{"label":"transport - motorbike","value":50},
{"label":"transport - bike","value":150}
];
var query1 = alasql('' +
'SELECT \'transport\' AS label, SUM(CAST([value] AS INT)) AS [value] ' +
'FROM ? ' +
'WHERE label LIKE \'%transport%\' ' +
'GROUP BY \'transport\' ' +
'',
[data]);
var query2 = alasql('' +
'SELECT label, SUM(CAST([value] AS INT)) AS [value] ' +
'FROM ? ' +
'WHERE label NOT LIKE \'%transport%\' ' +
'GROUP BY label' +
'',
[data]);
var queryUnion = alasql('' +
'SELECT \'transport\' AS label, SUM(CAST([value] AS INT)) AS [value] ' +
'FROM ? ' +
'WHERE label LIKE \'%transport%\' ' +
'GROUP BY \'transport\' ' +
'UNION ALL ' + //or UNION, same result!
'SELECT label, SUM(CAST([value] AS INT)) AS [value] ' +
'FROM ? ' +
'WHERE label NOT LIKE \'%transport%\' ' +
'GROUP BY label' +
'',
[data, data]);
$("#res").html("<br/>UNION IS WRONG (length is correct, but query2 results are missing!!!)!<br/>" + JSON.stringify(queryUnion) + " LENGTH: " + queryUnion.length);
$("#info").html("<br/>Query1 is correct:<br/>"
+ JSON.stringify(query1)
+ " LENGTH: " + query1.length
+ "<br/><br/>Query2 is correct<br/>" + JSON.stringify(query2)
+ " LENGTH: " + query2.length);