The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

ES generated SQL issue - Sorting is not working for a view with an aggregate column

Last post 05-28-2008, 2:22 PM by LBowers. 6 replies.
Sort Posts: Previous Next
  •  03-05-2008, 6:59 AM 8273

    ES generated SQL issue - Sorting is not working for a view with an aggregate column

    I have the following SQL view:

    Code:
    SELECT		dbo.Course.CourseId,
    dbo.Course.Date,
    dbo.UserDetail.Name AS 'CustomerName',
    dbo.Product.Name AS 'ProductName',
    COUNT(dbo.CourseDelegate.UserId) AS 'DelegateCount'
    FROM dbo.Course
    INNER JOIN dbo.Product ON dbo.Course.ProductCode = dbo.Product.ProductCode
    LEFT JOIN dbo.UserDetail ON dbo.Course.CustomerId = dbo.UserDetail.UserId
    LEFT JOIN dbo.CourseDelegate ON dbo.Course.Courseid = dbo.CourseDelegate.CourseId
    GROUP BY Course.CourseId, Course.[Date], UserDetail.Name, Product.Name
     

    And the following code is generated by ES, to page and sort the data:

    Code:
    WITH [withStatement] AS
    (
    SELECT DISTINCT c.[CourseId],c.[Date],c.[CustomerName],c.[ProductName],c.[DelegateCount],
    ROW_NUMBER() OVER( ORDER BY CustomerName ASC) AS ESRN
    FROM [vwCourseSummary] c
    )
    SELECT * FROM [withStatement] WHERE ESRN BETWEEN 1 AND 15

    To cut a long(ish) story short, it's not working. The resulting data is always sorted by the first column (CourseId). It appears that the DelegateCount column (the COUNT() in the view) is the source of the problem. If I remove the DelegateCount column from the second SQL statement (i.e. the ES generated statement), everything works fine. As soon as the DelegateCount column is re-introduced, sorting breaks.

    Does anyone have any idea why this is happening? Why does the introduction of an aggregate function in the view cause the sorting to break? Is this a bug with SQL Server? How can I fix / work around this problem?

    Thanks


    Leigh
  •  03-05-2008, 2:12 PM 8275 in reply to 8273

    Re: SQL Weirdness - Sorting is not working for a view with an aggregate column

    According to this article

    http://blogs.msdn.com/queryoptteam/archive/2006/05/02/588731.aspx

    the problem may be the SQL EntitySpaces generates. The inner ORDER BY only guarantees that ESRN will be calculated correctly. It does not guarantee that the outer SELECT will be returned in that order. Try this in Management Studio, and let me know if it fixes your issue:

    Code:
    WITH [withStatement] AS(	
        SELECT DISTINCT c.[CourseId],
            c.[Date],
            c.[CustomerName],
            c.[ProductName],
            c.[DelegateCount],	
            ROW_NUMBER() OVER( ORDER BY CustomerName ASC) AS ESRN	
        FROM [vwCourseSummary] c)
    SELECT * FROM [withStatement] WHERE ESRN BETWEEN 1 AND 15
    ORDER BY ESRN ASC

    In the meantime, does dbo.UserDetail.Name have a non-unique, non-clustered index? If not, adding one may help the optimizer come up with the execution plan you want, even without the outer ORDER BY.


    David Neal Parsons
    www.entityspaces.net
  •  03-06-2008, 5:07 AM 8280 in reply to 8275

    Re: ES generated SQL issue - Sorting is not working for a view with an aggregate column

    Ordering by ESRN does indeed work - I'd already given that a try in the Management Studio. I'll have a look at the indexes. I really need to get this working. This is in use in a DataGrid. It's the column sorting that's not working.

    Can you fix this in ES (i.e. by simply adding the "ORDER BY ESRN" to its generated statements)?

    Thanks David. 


    Leigh
  •  03-06-2008, 5:31 PM 8295 in reply to 8280

    Re: ES generated SQL issue - Sorting is not working for a view with an aggregate column

    We will indeed. However, the fix will have to be in ES2008 as no new 2007 version will be released. We should be able to give our user base some timelines after we get this weekends ES2008 Release out for CodeSmith, right now we're under the gun.

    EntitySpaces | Twitter | BLOG
  •  05-28-2008, 3:48 AM 9514 in reply to 8295

    Re: ES generated SQL issue - Sorting is not working for a view with an aggregate column

    Has this fix made it into ES2008?
    Leigh
  •  05-28-2008, 6:15 AM 9518 in reply to 9514

    Re: ES generated SQL issue - Sorting is not working for a view with an aggregate column

    The fix is in and tested for both SQL Server and Oracle. It should be in the next release.
    David Neal Parsons
    www.entityspaces.net
  •  05-28-2008, 2:22 PM 9531 in reply to 9518

    Re: ES generated SQL issue - Sorting is not working for a view with an aggregate column

    That's great news - Thanks David.
    Leigh
View as RSS news feed in XML