Expert T-SQL Window Functions in SQL Server

Book description

Expert T-SQL Window Functions in SQL Server takes you from any level of knowledge of windowing functions and turns you into an expert who can use these powerful functions to solve many T-SQL queries. Replace slow cursors and self-joins with queries that are easy to write and fantastically better performing, all through the magic of window functions.

First introduced in SQL Server 2005, window functions came into full blossom with SQL Server 2012. They truly are one of the most notable developments in SQL in a decade, and every developer and DBA can benefit from their expressive power in solving day-to-day business problems. Begin using windowing functions like ROW_NUMBER and LAG, and you will discover more ways to use them every day. You will approach SQL Server queries in a different way, thinking about sets of data instead of individual rows. Your queries will run faster, they will be easier to write, and they will be easier to deconstruct and maintain and enhance in the future.

Just knowing and using these functions is not enough. You also need to understand how to tune the queries. Expert T-SQL Window Functions in SQL Server explains clearly how to get the best performance. The book also covers the rare cases when older techniques are the best bet. Stop using cursors and self-joins to solve complicated queries. Become a T-SQL expert by mastering windowing functions.

  • Teaches you how to use all the window functions introduced in 2005 and 2012.
  • Provides realExpert T-SQL Window Functions in SQL Server takes you from any level of knowledge of windowing functions and turns you into an expert who can use these powerful functions to solve many T-SQL queries. Replace slow cursors and self-joins with queries that are easy to write and fantastically better performing, all through the magic of window functions.

    First introduced in SQL Server 2005, window functions came into full blossom with SQL Server 2012. They truly are one of the most notable developments in SQL in a decade, and every developer and DBA can benefit from their expressive power in solving day-to-day business problems. Begin using windowing functions like ROW_NUMBER and LAG, and you will discover more ways to use them every day. You will approach SQL Server queries in a different way, thinking about sets of data instead of individual rows. Your queries will run faster, they will be easier to write, and they will be easier to deconstruct and maintain and enhance in the future.

    Just knowing and using these functions is not enough. You also need to understand how to tune the queries. Expert T-SQL Window Functions in SQL Server explains clearly how to get the best performance. The book also covers the rare cases when older techniques are the best bet. Stop using cursors and self-joins to solve complicated queries. Become a T-SQL expert by mastering windowing functions.

  • Teaches you how to use all the window function-world examples that you can experiment with in your own database.
  • Explains how to get the best performance when using windowing functions.
  • Table of contents

    1. Cover
    2. Title
    3. Copyright
    4. Dedication
    5. Contents at a Glance
    6. Contents
    7. About the Authors
    8. About the Technical Reviewer
    9. Acknowledgments
    10. Author’s Note
    11. Chapter 1: Looking Through the Window
      1. Discovering Window Functions
      2. Thinking About the Window
      3. Understanding the OVER Clause
      4. Dividing Windows with Partitions
      5. Uncovering Special Case Windows
      6. Summary
    12. Chapter 2: Discovering Ranking Functions
      1. Using ROW_NUMBER
      2. Understanding RANK and DENSE_RANK
      3. Dividing Data with NTILE
      4. Solving Queries with Ranking Functions
        1. Deduplicating Data
        2. Finding the First N Rows of Every Group
        3. Solving the Islands Problem
        4. Solving the Bonus Problem
      5. Summary
    13. Chapter 3: Summarizing with Window Aggregates
      1. Using Window Aggregates
      2. Adding Window Aggregates to Aggregate Queries
      3. Using Window Aggregates to Solve Common Queries
        1. The Percent of Sales Problem
        2. The Partitioned Table Problem
      4. Creating Custom Window Aggregate Functions
      5. Summary
    14. Chapter 4: Tuning for Better Performance
      1. Using Execution Plans
      2. Using STATISTICS IO
      3. Understanding the Performance Implications of Window Aggregates
      4. Indexing to Improve the Performance of Window Functions
      5. Performing Time Comparisons
      6. Summary
    15. Chapter 5: Calculating Running and Moving Aggregates
      1. Adding ORDER BY to Window Aggregates
      2. Calculating Moving Totals and Averages
      3. Solving Queries Using Accumulating Aggregates
        1. The Last Good Value Problem
        2. The Subscription Problem
      4. Summary
    16. Chapter 6: Adding Frames to the Window
      1. Understanding Framing
      2. Applying Frames to Running and Moving Aggregates
      3. Measuring Performance
      4. Understanding the Logical Difference Between ROWS and RANGE
      5. Summary
    17. Chapter 7: Taking a Peek at Another Row
      1. Understanding LAG and LEAD
      2. Understanding FIRST_VALUE and LAST_VALUE
      3. Using the Offset Functions to Solve Queries
        1. The Year-Over-Year Growth Calculation
        2. The Gaps Problem
      4. Comparing Performance
        1. LAG and LEAD Performance
        2. FIRST_VALUE and LAST_VALUE PERFORMANCE
      5. Summary
    18. Chapter 8: Understanding Statistical Functions
      1. Using PERCENT_RANK and CUME_DIST
      2. Using PERCENTILE_CONT and PERCENTILE_DISC
      3. Comparing Statistical Functions to Older Methods
      4. Summary
    19. Chapter 9: Time Range Calculations and Trends
      1. Putting It All Together
        1. Percent of Parent
        2. Period-to-Date Calculations
      2. Averages, Moving Averages, and Rate-of-Change
        1. Same Period Prior Year
        2. Difference and Percent Difference
      3. Moving Totals and Simple Moving Averages
        1. Rate-of-Change Calculations
      4. Summary
    20. Index

    Product information

    • Title: Expert T-SQL Window Functions in SQL Server
    • Author(s): Kathi Kellenberger, Clayton Groom
    • Release date: March 2015
    • Publisher(s): Apress
    • ISBN: 9781484211038