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

Question: why to order by a field, it needs to be in the select? #963

Closed
marciogoularte opened this issue Oct 20, 2021 · 6 comments
Closed
Assignees
Labels
bug Something isn't working for grabs A community can grab for contribution

Comments

@marciogoularte
Copy link

Hi,

I'm using SQL Server and I have queries that make certain fields select and order by another

exemple:
base.Query<TEntity>(TableName(), fields: Field.From("Id", "Name"), orderBy: new List<OrderField>() { new OrderField("Created", Order.Descending) });

Throw exception here:
https://github.com/mikependon/RepoDB/blob/master/RepoDb.Core/RepoDb/StatementBuilders/BaseStatementBuilder.cs#L727

because without that it works too. What is the reason for this?

@marciogoularte marciogoularte added the question Further information is requested label Oct 20, 2021
@mikependon mikependon self-assigned this Oct 20, 2021
@mikependon
Copy link
Owner

We purposely have added this validation when composing a SQL statement.

Have you tried including the Created column from the list of fields you are querying? Technically, SQL Server could not order your result without it being listed as part of the selected columns.

@mikependon
Copy link
Owner

mikependon commented Oct 20, 2021

In short, the query below is not feasible.

SELECT Id, Name FROM TableName ORDER BY Created ASC;

But the query below would work.

SELECT Id, Name, Created FROM TableName ORDER BY Created ASC;

EDITED: Or, maybe, I am combining the sub query selections and other providers 😄

@marciogoularte
Copy link
Author

marciogoularte commented Oct 20, 2021

In my test I don't need Created (datetime). But I need to get Id and Name of the last or first users sorted by Created (datetime)

image
ignore the order of IDs because the created of id 3 was changed to old date
image

SQL Server sorts even if the column is not part of the select

with Created
image
image

but I don't need this column. I only need the Id and Name, but the order has to be the newest or the oldest

@mikependon
Copy link
Owner

Right, this is really a bug on the code. We will issue a fix for you, but we are glad if you can PR this simple fix.

It sounds like we need to remove the line 726-740, and also, the one in the QueryAll operation at line 792-806. It might affect some Unit Test, but the one we can always adjust to guard this logic.

Once fixed (either via PR or us), we will issue the release for you immediately.

Looking forward if you can PR this one mate.

@mikependon mikependon added bug Something isn't working for grabs A community can grab for contribution and removed question Further information is requested labels Oct 21, 2021
@mikependon mikependon pinned this issue Nov 9, 2021
@mikependon
Copy link
Owner

@SergerGood made the PR for this. The fix to this will be available the next releases > RepoDb v1.12.10-beta1.

@mikependon mikependon unpinned this issue Nov 19, 2021
@mikependon
Copy link
Owner

The fix for this is at PR #985

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working for grabs A community can grab for contribution
Projects
None yet
Development

No branches or pull requests

2 participants