Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SELECT TOP(100 PERCENT) in Derived Table Expand / Collapse
Author
Message
Posted Wednesday, October 10, 2012 1:44 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, September 19, 2014 11:51 AM
Points: 37, Visits: 146
Hi All

I'm trying to optimize a monstrous query in a report that a vendor supplied. Part of the query has SELECT TOP(100 PERCENT) which makes no sense to me.

...
RIGHT OUTER JOIN (
SELECT TOP ( 100 ) PERCENT
ts445.Date
,ts445.WorkCentre
,sts1.OpCode
,MAX(sts1.CostRateNT) AS CostRateNT
,MAX(sts1.AssistantRate) AS AssistantRate
,sts1.JobNo
FROM
Live_Costing.dbo.Timesheet AS ts445
INNER JOIN Live_Costing.dbo.TSTrans AS sts1 ON ts445.TimeSheetNo = sts1.TSID
GROUP BY
ts445.Date
,ts445.WorkCentre
,sts1.OpCode
,sts1.JobNo
ORDER BY
ts445.WorkCentre
,ts445.Date
,sts1.OpCode
) AS crsq1
RIGHT OUTER JOIN Live_Orders.dbo.OrderHeader AS oh
...


I see no point in the TOP (100 PERCENT) surely it'll just return all records, or am I missign something?

Regards

Mark
Post #1371098
Posted Wednesday, October 10, 2012 2:06 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 3:34 PM
Points: 3,109, Visits: 11,516
TOP is required when you have an ORDER BY in a derived table or view.






Post #1371110
Posted Wednesday, October 10, 2012 2:09 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:10 AM
Points: 13,230, Visits: 12,709
As Michael said you need the TOP when using a derived table and an ORDER BY.

What I don't understand is why there is a need to have an order by in the derived table. The order is pointless in this case. It seems that you could remove the top 100 percent AND the order by in the derived table.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1371112
Posted Wednesday, October 10, 2012 2:30 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, September 19, 2014 11:51 AM
Points: 37, Visits: 146
Sean Lange (10/10/2012)
As Michael said you need the TOP when using a derived table and an ORDER BY.

What I don't understand is why there is a need to have an order by in the derived table. The order is pointless in this case. It seems that you could remove the top 100 percent AND the order by in the derived table.


Thanks for replies. I may have figured it out. It looks like another part of the query relies on the records being a specific order so it can do some differential calcs. I'm sure there's a better way, jut got to try and figure out everything else the query is doing

Regards

Mark
Post #1371119
Posted Wednesday, October 10, 2012 10:32 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 2:06 AM
Points: 1,768, Visits: 8,318
Blurgh, sick in my mouth a little.

Im no sql purist by any means, but this *cant* work. ie relying on top 100 percent to order ,(at least in the long term)

Consider these two queries....

select *
from (Select 1 as rown) a
right join (
select top(100) percent object_id,name
from sys.objects order by name
) as b on a.rown<>b.object_id
go
select *
from (Select 1 as rown) a
right join (
select top(99.99) percent object_id,name
from sys.objects order by name
) as b on a.rown<>b.object_id

Look at the plans, the top 100 percent make so little sense it gets optimized out and the results come back in a 'random' order, or at least in SQL 2012 it does.

Ordering within SQL can and should only be applied to the final result set.

There is a 'fix' ( i hate to use that word for this issue ) in 2008
http://support.microsoft.com/kb/926292

At the back of my mind i think there may even be a trace flag to enable this too ( im sure someone will correct me if wrong)




Clear Sky SQL
My Blog
Kent user group
Post #1371205
Posted Wednesday, October 10, 2012 10:47 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, October 16, 2014 10:02 AM
Points: 1,372, Visits: 1,566
mark 4643 (10/10/2012)
Hi All

I'm trying to optimize a monstrous query in a report that a vendor supplied. Part of the query has SELECT TOP(100 PERCENT) which makes no sense to me.

I see no point in the TOP (100 PERCENT) surely it'll just return all records, or am I missign something?

Regards

Mark


You cannot put order by clause alone inside a subquery; in order to do that you need to specify top 100 percent or you can use FOR XML clause. This fundamental holds true for views, inline functions, derived tables, subqueries, and common table expressions where the order by clause is invalid without TOP or FOR XML operators.

Hope this helps


~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter

Post #1371206
Posted Thursday, October 11, 2012 1:45 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 10:44 AM
Points: 1,037, Visits: 6,966
CREATE VIEW (Transact-SQL) SQL Server 2005 states the following: "The ORDER BY clause is used only to determine the rows that are returned by the TOP clause in the view definition. The ORDER BY clause does not guarantee ordered results when the view is queried, unless ORDER BY is also specified in the query itself." Most folks assume that the rules which apply to views apply to derived tables also.
SELECT TOP (100 PERCENT) is optimised out as Dave B stated, it's meaningless. I think you can still cheat with SELECT TOP 2147483647 or some other number safely bigger than the anticipated rowcount of the result set.


It looks like another part of the query relies on the records being a specific order so it can do some differential calcs.


Even if the ORDER BY worked, it would be unlikely to do anything except introduce an unnecessary sort; what would happen to the results from this derived table if the next join operator was anything other than a merge join without a presort? For the query to work as designed, you'd need merge joins (without presorts) all the way between this output and "another part of the query".
So, if the query results are correct it's certainly not by design - which means it works by accident.





Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #1371250
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse