Skip to content

Commit

Permalink
fix: distinct and top position in tsql
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Mar 31, 2024
1 parent 9da9d17 commit 74de53f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pegjs/transactsql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,8 +1345,8 @@ cte_column_definition
select_stmt_nake
= __ cte:with_clause? __ KW_SELECT ___
opts:option_clause? __
top: top_clause? __
d:KW_DISTINCT? __
top: top_clause? __
c:column_clause __
f:from_clause? __
w:where_clause? __
Expand Down
3 changes: 1 addition & 2 deletions src/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ function selectToSQL(stmt) {
where,
} = stmt
const clauses = [withToSQL(withInfo), 'SELECT', toUpper(asStructVal)]
clauses.push(topToSQL(top))
if (Array.isArray(options)) clauses.push(options.join(' '))
clauses.push(distinctToSQL(distinct), columnsToSQL(columns, from))
clauses.push(distinctToSQL(distinct), topToSQL(top), columnsToSQL(columns, from))
const { position } = into
let intoSQL = ''
if (position) intoSQL = commonOptionConnector('INTO', selectIntoToSQL, into)
Expand Down
4 changes: 3 additions & 1 deletion test/transactsql.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ describe('transactsql', () => {
}

it('should support select top n', () => {
const sql = 'select top 3 * from tableA'
let sql = 'select top 3 * from tableA'
expect(getParsedSql(sql)).to.equal('SELECT TOP 3 * FROM [tableA]')
sql = `SELECT DISTINCT TOP 2 FirstName, LastName FROM Students WHERE GraduationYear = 2003 ORDER BY GradePointAverage DESC;`
expect(getParsedSql(sql)).to.equal('SELECT DISTINCT TOP 2 [FirstName], [LastName] FROM [Students] WHERE [GraduationYear] = 2003 ORDER BY [GradePointAverage] DESC')
})

it('should support select top n percent', () => {
Expand Down

0 comments on commit 74de53f

Please sign in to comment.