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 SQL error when ordering on subquery result #236

Open
jeff-chastain opened this issue Sep 6, 2023 · 0 comments
Open

Pagination SQL error when ordering on subquery result #236

jeff-chastain opened this issue Sep 6, 2023 · 0 comments

Comments

@jeff-chastain
Copy link

jeff-chastain commented Sep 6, 2023

Suppose I have a Quick result set where one of the properties is derived from a sub-query. If I attempt to paginate and order that result set by the sub-query derived property and error is thrown because the ROW_NUMBER() OVER (...) clause is at the same level in the query as the sub-query property.

This is the current SQL generated by QB in this scenario ... (note the balanceDue derived column right next to the ROW_NUMBER())

SELECT	* 
FROM	(
		SELECT	[tblBK_Travel].*, 
			(
				(
					SELECT COALESCE( SUM( tblBK_Travel_Cost.Amount ), 0 )
					FROM tblBK_Travel_Cost 
					WHERE tblBK_Travel.Travel_ID = tblBK_Travel_Cost.Travel_ID
				) - (
					SELECT COALESCE( SUM(tblBK_Travel_Payment.Amount), 0 ) 
					FROM tblBK_Travel_Payment 
					WHERE tblBK_Travel.Travel_ID = tblBK_Travel_Payment.Travel_ID
				)
			) AS balanceDue, 
			ROW_NUMBER() OVER (
				ORDER BY CASE 
					WHEN balanceDue IS NULL OR balanceDue = '' THEN 1 ELSE 0 END, 
					[balanceDue] DESC, 
					[tblBK_Travel].[Travel_ID] DESC
				) AS [QB_RN] 

		FROM	[tblBK_Travel] 

		WHERE	[tblBK_Travel].[Trip_ID] = '1664100' 
	) [QB_RESULTS] 

WHERE	[QB_RN] > 0 
		AND [QB_RN] <= 25 
ORDER BY 
		[QB_RN] ASC

If instead, the base query is wrapped in a SELECT * with the ROW_NUMBER() OVER (...) applied at that level, then derived columns can be used without issue in the order by.

SELECT	* 
FROM	(
			SELECT	*,
					ROW_NUMBER() OVER (
						ORDER BY CASE 
							WHEN balanceDue IS NULL OR balanceDue = '' THEN 1 ELSE 0 END, 
							[balanceDue] DESC, 
							[Travel_ID] DESC
						) AS [QB_RN]

			FROM	(
						SELECT	[tblBK_Travel].*, 
							(
								(
									SELECT COALESCE( SUM( tblBK_Travel_Cost.Amount ), 0 )
									FROM tblBK_Travel_Cost 
									WHERE tblBK_Travel.Travel_ID = tblBK_Travel_Cost.Travel_ID
								) - (
									SELECT COALESCE( SUM(tblBK_Travel_Payment.Amount), 0 ) 
									FROM tblBK_Travel_Payment 
									WHERE tblBK_Travel.Travel_ID = tblBK_Travel_Payment.Travel_ID
								)
							) AS balanceDue

						FROM	[tblBK_Travel] 

						WHERE	[tblBK_Travel].[Trip_ID] = '1664100' 

					) [QB_RESULTS] 

		) [QB_PAGINATED_RESULTS] 

WHERE	[QB_RN] > 0 
		AND [QB_RN] <= 25 
ORDER BY 
		[QB_RN] ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants