diff --git a/src/38query.js b/src/38query.js index 926555a62b..dec72674a4 100755 --- a/src/38query.js +++ b/src/38query.js @@ -202,8 +202,10 @@ function queryfn3(query) { var d = query.selectgfn(g, query.params, alasql); for (const key in query.groupColumns) { - if (query.groupColumns[key] !== key && d[query.groupColumns[key]]) + // ony remove columns where the alias is also not a column in the result + if (query.groupColumns[key] !== key && d[query.groupColumns[key]] && !query.groupColumns[query.groupColumns[key]]){ delete d[query.groupColumns[key]]; + } } query.data.push(d); } diff --git a/test/test1820.js b/test/test1820.js new file mode 100644 index 0000000000..a3babed2c4 --- /dev/null +++ b/test/test1820.js @@ -0,0 +1,26 @@ +if (typeof exports === 'object') { + var assert = require('assert'); + var alasql = require('..'); +} else { + __dirname = '.'; +} + +describe('Test 1820 - SELECT query (a AS b, b AS c)', function () { + + it('1. Select query where alias of one column is also a column name in the result set', function (done) { + let item1 = { a: 1, b: "hello" }; + let item2 = { a: 2, b: "" }; + + var res = alasql('SELECT a as b, b as c FROM ? GROUP BY a,b', [[item1, item2]]); + + assert.deepEqual(res, [{ + b: 1, + c: "hello" + }, { + b: 2, + c: "" + }]); + + done(); + }); +});