Skip to content

Query: Simplify aliases for complex projection if being used in order by clause #6703

Closed

Description

@smitpatel

For query like this

selection = db.Nodes.Include(x => x.NodeRecords)
        .Select(s => new HighNode()
        {
            Name = s.Name,
            Change = s.NodeRecords.OrderByDescending(o => o.NodeRecordId).Skip(days).FirstOrDefault().Change
        }).OrderByDescending(t => t.Change).Take(100).ToList();

We generate Sql like this

SELECT TOP(@__p_1) [x].[Name], (
    SELECT [o0].[Change]
    FROM [NodeRecords] AS [o0]
    WHERE [x].[Name] = [o0].[Node_Name]
    ORDER BY [o0].[NodeRecordId] DESC
    OFFSET @__days_0 ROWS FETCH NEXT 1 ROWS ONLY
)
FROM [Nodes] AS [x]
ORDER BY (
    SELECT [o].[Change]
    FROM [NodeRecords] AS [o]
    WHERE [x].[Name] = [o].[Node_Name]
    ORDER BY [o].[NodeRecordId] DESC
    OFFSET @__days_0 ROWS FETCH NEXT 1 ROWS ONLY
) DESC

We can just alias the subquery in projection and use alias in order by clause as enhancement.

For model and more info see #6257

Activity

  1. added this to the milestone on Oct 7, 2016
  2. smitpatel commented on Oct 25, 2016

    @smitpatel
    ContributorAuthor

    Another expression to alias is COALESCE

  3. smitpatel commented on Dec 6, 2016

    @smitpatel
    ContributorAuthor

    This is a necessity for the case of coalesce when it is inside a subquery because we need to copy order by on outer query but the columns for coalesce are not projected out hence generates invalid query like this.
    Disabling test Select_take_skip_null_coalesce_operator

    exec sp_executesql N'SELECT [t].*
    FROM (
        SELECT TOP(@__p_0) [c].[CustomerID], [c].[CompanyName], COALESCE([c].[Region], N''ZZ'') AS [c0]
        FROM [Customers] AS [c]
        ORDER BY COALESCE([c].[Region], N''ZZ'')
    ) AS [t]
    ORDER BY COALESCE([t].[Region], N''ZZ'')
    OFFSET @__p_1 ROWS',N'@__p_0 int,@__p_1 int',@__p_0=10,@__p_1=5
    
  4. smitpatel commented on Mar 21, 2017

    @smitpatel
    ContributorAuthor

    @anpete @maumar - I was looking this in my current work. For complex expressions, we are able to alias the projection and use it in order by but for subquery it is failing because we generate different select expression (essentially the table aliases are different) because our subquerymodelvisitor will run twice on same qm. Thoughts where we should optimize this?

  5. smitpatel commented on Mar 29, 2017

    @smitpatel
    ContributorAuthor

    @anpete @maumar - For all cases except subquery, alias-ing of ordering should be working. As explained above, to alias select expression in order by we need that subqueryExpression referencing same QM generating same SelectExpression. Do we want to keep this issue open now? Or merge into the larger issue?

  6. divega commented on May 8, 2017

    @divega
    Contributor

    Do we want to keep this issue open now? Or merge into the larger issue?

    @smitpatel this got closed automatically when you pushed the commit, that might explain why no one answered 😄 Should it be reopened? Otherwise closed as fixed?

  7. changed the title [-]Query: Alias subquery if being used in order by clause[/-] [+]Query: Alias complex projection if being used in order by clause[/+] on May 8, 2017
  8. smitpatel commented on May 8, 2017

    @smitpatel
    ContributorAuthor

    We do alias complex projections appearing in projection & order by both and refer to the alias in order by clause.
    For subqueries, the generated select expressions are different so we have no way to match them and use alias.
    All possible work for this issue has been done at this point.

  9. changed the title [-]Query: Alias complex projection if being used in order by clause[/-] [+]Query: Simplify aliases for complex projection if being used in order by clause[/+] on May 9, 2017
  10. modified the milestones: , 2.0.0 on Oct 15, 2022
  11. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions