SQL Server Performance Forum – Threads Archive
order by problemHi Everyone,
This is probably a known problem to some but I’ve never encountered it.
I have a user who is running a select into statement with a group by then order by statement. When she views the results, they are not ordered correctly in the final table. All SQL is correct and if you run a second select * to screen with same group and ordering, it looks fine. What’s up ?
Ken, Maybe your "final table" has existing index. Jon M
The order in which the rows are stored in a table depends on the primary key on the table. It sorts the data based on the index on this primary key and not the order in which you enter the data into it.
I don’t think that’s it. It turns out once the user changed the select into tablename, it worked fine. I think maybe when you delete a table then select into the same tablename, there may be an update statistics problem ? Does this sound far fetched to anyone ?
How does your user view the final results?
Ordering is theoretically more or less unimportant in a table.
And although there might be a clustered index on the final table which works in 99% of all cases, the only way to get the expected results is to use an ORDER BY clause.