简体   繁体   中英

ORDER BY in a Sql Server 2008 view

we have a view in our database which has an ORDER BY in it. Now, I realize views generally don't order, because different people may use it for different things, and want it differently ordered. This view however is used for a VERY SPECIFIC use-case which demands a certain order. (It is team standings for a soccer league.)

The database is Sql Server 2008 Express, v.10.0.1763.0 on a Windows Server 2003 R2 box.

The view is defined as such:

CREATE VIEW season.CurrentStandingsOrdered
AS
    SELECT TOP 100 PERCENT *, season.GetRanking(TEAMID) RANKING   
    FROM season.CurrentStandings 
    ORDER BY 
        GENDER, TEAMYEAR, CODE, POINTS DESC, 
        FORFEITS, GOALS_AGAINST, GOALS_FOR DESC, 
        DIFFERENTIAL, RANKING

It returns:

GENDER, TEAMYEAR, CODE, TEAMID, CLUB, NAME,  
WINS, LOSSES, TIES, GOALS_FOR, GOALS_AGAINST,  
DIFFERENTIAL, POINTS, FORFEITS, RANKING

Now, when I run a SELECT against the view, it orders the results by GENDER, TEAMYEAR, CODE, TEAMID . Notice that it is ordering by TEAMID instead of POINTS as the order by clause specifies.

However, if I copy the SQL statement and run it exactly as is in a new query window, it orders correctly as specified by the ORDER BY clause.

SQL Server 2005 ignores TOP 100 PERCENT by design.

Try TOP 2000000000 instead.

Now, I'll try and find a reference... I was at a seminar presented by Itzak Ben-Gan who mentioned it

Found some...

Kimberly L. Tripp

"TOP 100 Percent ORDER BY Considered Harmful"

In this particular case, the optimizer recognizes that TOP 100 PERCENT qualifies all rows and does not need to be computed at all.

The order of rows returned by a view with an ORDER BY clause is never guaranteed. If you need a specific row order, you must specify where you select from the view.

See this the note at the top of this Book On-Line entry.

Just use :

" Top (99) Percent "

or

"Top (a number 1000s times more than your data rows like 24682468123)" it works! just try it.

In SQL server 2008, ORDER BY is ignored in views that use TOP 100 PERCENT. In prior versions of SQL server, ORDER BY was only allowed if TOP 100 PERCENT was used, but a perfect order was never guaranteed. However, many assumed a perfect order was guaranteed. I infer that Microsoft does not want to mislead programmers and DBAs into believing there is a guaranteed order using this technique.

An excellent comparative demonstration of this inaccuracy, can be found here...

http://blog.sqlauthority.com/2009/11/24/sql-server-interesting-observation-top-100-percent-and-order-by

Oops, I just noticed that this was already answered. But checking out the comparative demonstration is worth a look anyway.

Microsoft has fixed this. You have patch your sql server

http://support.microsoft.com/kb/926292

I found an alternative solution.

My initial plan was to create a 'sort_order' column that would prevent users from having to perform a complex sort.

I used a windowed function ROW_NUMBER. In the ORDER BY clause, I specified the default sort order that I needed (just as it would have been in the ORDER BY of a SELECT statement).

I get several positive outcomes:

  1. By default, the data is getting returned in the default sort order I originally intended (this is probably due to the windowed function having to sort the data prior to assigning the sort_order value)

  2. Other users can sort the data in alternative ways if they choose to

  3. The sort_order column is there for a very specific sort need, making it easier for users to sort the data should whatever tool they use rearranges the rowset.

Note: In my specific application, users are accessing the view via Excel 2010, and by default the data is presented to the user as I had hoped without further sorting needed.

Hope this helps those with a similar problem.

Cheers, Ryan

run a profiler trace on your database and see the query that's actually being run when you query your view.

You also might want to consider using a stored procedure to return the data from your view, ordered correctly for your specific use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM