Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination fails with distinct (Grammer: MS SQL SERVER) #213

Open
srconklin opened this issue Feb 21, 2022 · 8 comments
Open

Pagination fails with distinct (Grammer: MS SQL SERVER) #213

srconklin opened this issue Feb 21, 2022 · 8 comments
Milestone

Comments

@srconklin
Copy link

When drafting a QB query with distinct() it constructs an invalid query select list:

`

Message stringIncorrect syntax near '*'. string Incorrect syntax near '*'.
string Incorrect syntax near '*'.
NativeErrorCode number102 number 102
number 102
queryError stringSELECT COUNT(DISTINCT *) AS "aggregate" FROM [batches] INNER JOIN [projects] ON [projects].[project_id] = [batches].[project_id] INNER JOIN [users] ON [users].[user_id] = [batches].[owner_user_id] INNER JOIN [statuses] ON [statuses].[statusID] = [batches].[status] INNER JOIN [labels] ON [labels].[label_key] = [batches].[department] INNER JOIN [assignments] ON [assignments].[batch_id] = [batches].[batch_id] WHERE [labels].[type] = 'dept_code' AND [batches].[status] = '1' AND [batches].[department] = '1' string SELECT COUNT(DISTINCT *) AS "aggregate" FROM [batches] INNER JOIN [projects] ON [projects].[project_id] = [batches].[project_id] INNER JOIN [users] ON [users].[user_id] = [batches].[owner_user_id] INNER JOIN [statuses] ON [statuses].[statusID] = [batches].[status] INNER JOIN [labels] ON [labels].[label_key] = [batches].[department] INNER JOIN [assignments] ON [assignments].[batch_id] = [batches].[batch_id] WHERE [labels].[type] = 'dept_code' AND [batches].[status] = '1' AND [batches].[department] = '1'
string SELECT COUNT(DISTINCT *) AS "aggregate" FROM [batches] INNER JOIN [projects] ON [projects].[project_id] = [batches].[project_id] INNER JOIN [users] ON [users].[user_id] = [batches].[owner_user_id] INNER JOIN [statuses] ON [statuses].[statusID] = [batches].[status] INNER JOIN [labels] ON [labels].[label_key] = [batches].[department] INNER JOIN [assignments] ON [assignments].[batch_id] = [batches].[batch_id] WHERE [labels].[type] = 'dept_code' AND [batches].[status] = '1' AND [batches].[department] = '1'
`

I found that by removing the reference to the DISTINCT keyword in the following function, the paginate works as expected:
Wondering why this built to test if distinct is present?

`
private string function compileAggregate( required QueryBuilder query, required struct aggregate ) {
if ( aggregate.isEmpty() ) {
return "";
}
return "SELECT #uCase( aggregate.type )#(#query.getDistinct() ? "DISTINCT " : ""##wrapColumn( aggregate.column )#) AS ""aggregate""";
}

`

@srconklin
Copy link
Author

This appears to be still happening in the latest version

@elpete
Copy link
Collaborator

elpete commented Jul 11, 2022

Can you provide me with a query that is breaking?

@srconklin
Copy link
Author

SELECT COUNT(DISTINCT *) AS "aggregate" FROM [batches] INNER JOIN [projects] ON [projects].[project_id] = [batches].[project_id] INNER JOIN [users] ON [users].[user_id] = [batches].[owner_user_id] INNER JOIN [statuses] ON [statuses].[statusID] = [batches].[status] INNER JOIN [labels] ON [labels].[label_key] = [batches].[department] INNER JOIN [assignments] ON [assignments].[batch_id] = [batches].[batch_id] WHERE [labels].[type] = 'dept_code' AND [batches].[status] = '1' AND [batches].[department] = '1'

@elpete
Copy link
Collaborator

elpete commented Jul 11, 2022

Is that the correct SQL or incorrect? Also, can I get the qb code that is generating that?

@srconklin
Copy link
Author

that is the incorrect sql. (distinct can't be in the aggregate function () )

@srconklin
Copy link
Author

srconklin commented Jul 11, 2022

local.projects = _wirebox.getInstance( "QueryBuilder@qb" ) .select( [ "projects.project_id", "projects.project_number", "projects.project_name", "projects.project_description", "projects.budget", "batches.date_due", "batches.department", "batches.batch_number", "batches.batch_description", "batches.batch_id AS the_batch_id", "batches.owner_user_id", "users.full_name as owner", "statuses.status" ] ) .distinct() .selectRaw( "COALESCE(trim(batches.client_lastname), '') + case when len(COALESCE(trim(batches.client_firstname),'')) > 0 then ', ' else '' end + COALESCE(trim(batches.client_firstname),'') as client" ) .selectRaw( "'##' + cast(batches.batch_number as varchar(3)) + ' (' + labels.txt + ') ' + isnull(batches.batch_description, '') as batch" ) .selectRaw( "case when batches.status = 1 then batches.time_created else case when batches.status = 2 then batches.time_created else case when batches.status = 3 then batches.time_recharged else 'Unknown' end end end as statusDate " ) .subSelect( "actualCharge", function( q ){ q.selectRaw( "SUM(total)" ) .from( "entries" ) .whereIn( "batch_id", ( q ) => { q.select( "batch_id" ) .from( "batches" ) .whereColumn( "batches.project_id", "projects.project_id" ) }); } ) .subSelect( "total", function( q ){ q.selectRaw( "SUM(total)" ) .from( "entries" ) .whereColumn( "entries.batch_id", "batches.batch_id" ); } ) .from( "batches" ) .join( "projects", "projects.project_id", "batches.project_id" ) .join( "users", "users.user_id", "batches.owner_user_id" ) .join( "statuses", "statuses.statusID", "batches.status" ) .join( "labels", "labels.label_key", "=", "batches.department" ) .join( "assignments", "assignments.batch_id", "batches.batch_id" ) .where( "labels.type", [ "dept_code" ] ) // .where( "batches.status", arguments.status ) .when( len( arguments.status ), function( q ){ q.where( "batches.status", status ); } ) .when( len( arguments.projectID ), function( q ){ q.where( "batches.project_id", projectID ); } ) .when( len( arguments.qcrit ), function( q ){ searchableColumns.each( ( item ) => { q.wherelike( item, "%#qcrit#%", "or"); }); } );

@srconklin
Copy link
Author

looks like the formatting is lost even using the code formatting option.

@srconklin
Copy link
Author

srconklin commented Jul 11, 2022

FYI. In case I did not clarify. This error occurs when invoking the paginate method.
The qb code is quite long but the issue can be seen in a much simpler example by simply adding distinct() to the qb configuration

@elpete elpete added this to the v9.0.0 milestone Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants