|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, February 18, 2008 2:25 PM
Points: 43,
Visits: 71
|
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Yesterday @ 6:03 AM
Points: 4,787,
Visits: 1,335
|
|
A very simple and good article. Well explained and very constructive article.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 8:40 AM
Points: 1,078,
Visits: 848
|
|
As you can see CTEs can be very useful. I have found them particularly handy when cleaning up demoralized tables What are "demoralized tables"? I didn't know they can have such character. ;)
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, April 14, 2013 8:55 AM
Points: 1,383,
Visits: 1,212
|
|
I see the value of CTEs, but I'm not sure I agree with the example... wouldn't it be better written (more efficient?) using a single aggregate query and a TOP clause?
SELECT TOP 1 dept_id, n FROM ( SELECT dept_id, count(*) AS n FROM employee GROUP BY dept_id ) AS a ORDER BY n DESC I guess the question for me is: while CTEs provide a nice syntax for repeated and recursive expressions, is the SQL optimizer actually able to use them as efficiently as a statement designed to avoid repetitive expressions in the first place?
I tried this on a very simple dataset (comparing all three statements), and found that the total cost of the queries, in all three cases, was the same. The query plans were slightly more complicated for the original statement and CTE statement, and slightly simpler for the statement above (single Sort/TopN Sort instead of Sort -> Filter -> Top) - unfortunately I don't have a large dataset to test on!
My instinct would be: Try to rewrite your statement to use joins and derived tables rather than subqueries, before moving to CTEs as a way of simplifying your subqueries.
Does anyone know better one way or the other?
http://poorsql.com for T-SQL formatting: free as in speech, free as in beer, free to run in SSMS or on your version control server - free however you want it.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, June 21, 2007 10:00 PM
Points: 3,
Visits: 1
|
|
| There was no explanation about why the CTE was a better approach than any of the others
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Yesterday @ 6:03 AM
Points: 4,787,
Visits: 1,335
|
|
The query provided by Tao will cost less in a large database then CTE no doubt........
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, August 25, 2008 9:58 AM
Points: 1,
Visits: 17
|
|
was there any source code or at least a method to create the database and table(s) referenced? It would be helpful in testing running the provided code. Good article otherwise I also liked the demoralized term.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 9:57 PM
Points: 32,906,
Visits: 26,790
|
|
Doing my morning article "skimming" and saw this one. In the "Conclusion" it's stated "This way I avoid using a GROUP BY "... and the example right above it has two CTE's... and each has a GROUP BY in it. I admit that's it's just a "skim" so far (I'll read it in depth tonight), but just exactly what do you mean by you avoided the use of GROUP BY?
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 02, 2012 12:20 PM
Points: 1,
Visits: 42
|
|
The query provided by Tao is fine and was the first solution i thought of when i read the example problem. However, the CTE example will effortlessly resolve the issue of two or more departments having the same number of employees:
If two departments have n employees and are tied for having the most in the company, both should be displayed. Tao's use of the TOP 1 clause in this case will choose one of the departments arbitrarily for display and produce a less-than desirable result.
Still, i'm not all that happy with the example. The issue can be solved many other other ways besides using CTE's though it does present the CTE concept very cleanly.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 1:55 PM
Points: 15,442,
Visits: 9,571
|
|
Tao Klerks (3/18/2008) ...My instinct would be: Try to rewrite your statement to use joins and derived tables rather than subqueries, before moving to CTEs as a way of simplifying your subqueries.
Does anyone know better one way or the other?
CTEs work pretty much the same way as derived tables. They have the advantage of "build once, reference many", but in cases where you just use the derived table once, a CTE and a derived table are essentially the same, and will resolve the same way in the query engine.
In those cases, I currently use CTEs instead of derived tables, because I find they make for more readable code. The final select statement isn't as cluttered. No performance reason, just easier to read.
Also, since I am using CTEs in the places where they have significant advantages over derived tables (self-reference, multi-reference, query-of-query), using them in other places is more consistent, and that helps me to set and maintain a standard.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|