Results 1 to 4 of 4

Thread: Derived Tables

  1. #1
    Join Date
    Oct 2014
    Posts
    268
    Provided Answers: 6

    Answered: Derived Tables

    I noticed a lot of my co workers use these A LOT to aggregating data. It seems to be the biggest go to.

    Thoughts?

    I recently came from a shop that used temp tables and CTE's A LOT, but these Derived tables are more effective IMO. You can roll up data in virtual tables and remove duplicates that way. Using temp tables and CTE's seems like sloppy coding in relation to Derived Tables.

    What do you mainly use when aggregating data?

  2. Best Answer
    Posted by Pat Phelan

    "My preferences (for what they are worth):
    1. CTE
    2. VIEW
    3. Derived table
    4. Temp table (either #temp or ##temp)

    There are costs and benefits associated with each of them, and some of those costs and benefits vary from one version of SQL Server to another. As a general rule, that list matches both efficiency (how long does it take SQL Server to materialize the data that you specified) and also portability (being able to use on other SQL dialects).

    -PatP"


  3. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    What have you got against CTEs?
    Can you clarify what you mean in this instance as a "derived table"?
    George
    Home | Blog

  4. #3
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    I think he/she means tables with queries in the FROM clause as tables. I find this is approach to perform so poorly that I stopped using derived tables years ago. It also does you no favors if you need the same derived table twice because you have to invoke it twice. It also makes your code very ugly and messy and hard to read.

    The big heaping piece of crap BI application I just inherited has this derived table approach all over the place as well as just about every other coding convention I consider bad practice.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  5. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    My preferences (for what they are worth):
    1. CTE
    2. VIEW
    3. Derived table
    4. Temp table (either #temp or ##temp)

    There are costs and benefits associated with each of them, and some of those costs and benefits vary from one version of SQL Server to another. As a general rule, that list matches both efficiency (how long does it take SQL Server to materialize the data that you specified) and also portability (being able to use on other SQL dialects).

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •