You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I attempt to paginate a collection containing a GROUP BY clause I get a SQLServer error. The generated SQL contains an ORDER BY [PRIMARY_KEY] while the primary key is not part of select or group by.
In issue #167, @metaskills explains that SQLServer requires an order. So when no order is specified, the adapter sets the table's Primary Key as the key to order with. This does not work when you group records though.
I assume that the fix is to sort by the first column of the GROUP BY clause when the results are grouped.
Does that make sense?
Example:
Receipt.group(:PurchaseOrderId).offset(3).allReceiptLoad(149.4ms)EXECsp_executesqlN'SELECT TOP (9223372036854775807) [__rnt].* FROM (SELECT ROW_NUMBER() OVER (ORDER BY [Receipts].[Id] ASC) AS [__rn], [Receipts].* FROM [Receipts] GROUP BY PurchaseOrderId ) AS [__rnt] WHERE [__rnt].[__rn] > (3)ORDER BY [__rnt].[__rn] ASC'ActiveRecord::StatementInvalid: TinyTds::Error:
Column'Receipts.Id'isinvalidintheselectlistbecauseitis not containedineitheranaggregatefunctionortheGROUPBYclause.:
EXECsp_executesqlN'SELECT TOP (9223372036854775807) [__rnt].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [Receipts].[Id] ASC) AS [__rn], [Receipts].* FROM [Receipts] GROUP BY PurchaseOrderId ) AS [__rnt] WHERE [__rnt].[__rn] > (3) ORDER BY [__rnt].[__rn] ASC'
The text was updated successfully, but these errors were encountered:
Hello,
When I attempt to paginate a collection containing a
GROUP BY
clause I get a SQLServer error. The generated SQL contains anORDER BY [PRIMARY_KEY]
while the primary key is not part ofselect
orgroup by
.In issue #167, @metaskills explains that SQLServer requires an order. So when no order is specified, the adapter sets the table's Primary Key as the key to order with. This does not work when you group records though.
I assume that the fix is to sort by the first column of the
GROUP BY
clause when the results are grouped.Does that make sense?
Example:
The text was updated successfully, but these errors were encountered: