Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SQL Query Design Patterns and Best Practices

You're reading from   SQL Query Design Patterns and Best Practices A practical guide to writing readable and maintainable SQL queries using its design patterns

Arrow left icon
Product type Paperback
Published in Mar 2023
Publisher Packt
ISBN-13 9781837633289
Length 270 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (6):
Arrow left icon
Chi Zhang Chi Zhang
Author Profile Icon Chi Zhang
Chi Zhang
Steven Hughes Steven Hughes
Author Profile Icon Steven Hughes
Steven Hughes
Shabbir Mala Shabbir Mala
Author Profile Icon Shabbir Mala
Shabbir Mala
Dennis Neer Dennis Neer
Author Profile Icon Dennis Neer
Dennis Neer
Leslie Andrews Leslie Andrews
Author Profile Icon Leslie Andrews
Leslie Andrews
Ram Babu Singh Ram Babu Singh
Author Profile Icon Ram Babu Singh
Ram Babu Singh
+2 more Show less
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Part 1: Refining Your Queries to Get the Results You Need
2. Chapter 1: Reducing Rows and Columns in Your Result Sets FREE CHAPTER 3. Chapter 2: Efficiently Aggregating Data 4. Chapter 3: Formatting Your Results for Easier Consumption 5. Chapter 4: Manipulating Data Results Using Conditional SQL 6. Part 2: Solving Complex Business and Data Problems in Your Queries
7. Chapter 5: Using Common Table Expressions 8. Chapter 6: Analyze Your Data Using Window Functions 9. Chapter 7: Reshaping Data with Advanced Techniques 10. Chapter 8: Impact of SQL Server Security on Query Results 11. Part 3: Optimizing Your Queries to Improve Performance
12. Chapter 9: Understanding Query Plans 13. Chapter 10: Understanding the Impact of Indexes on Query Design 14. Part 4: Working with Your Data on the Modern Data Platform
15. Chapter 11: Handling JSON Data in SQL Server 16. Chapter 12: Integrating File Data and Data Lake Content with SQL 17. Chapter 13: Organizing and Sharing Your Queries with Jupyter Notebooks 18. Index 19. Other Books You May Enjoy Appendix: Preparing Your Environment

Working with the PIVOT operator

A PIVOT operator, simply put, transforms a table output column into rows. It rotates a table-valued expression (a table-valued expression returns output as a result set/table) by turning unique values from a selected column into multiple columns and aggregates the remaining column values in the final table output.

Figure 7.1 – Basic understanding of the PIVOT operator

Figure 7.1 – Basic understanding of the PIVOT operator

The PIVOT operator is very similar to a CASE statement, but much simpler and more easily readable for the user.

Here is the syntax for it:

SELECT
<Unique Column Name(s)>
FROM
<SELECT query to produce data from table(s)>
PIVOT
(
<column to be aggregated, e.g., COUNT, AVG, etc.>
FOR
<Unique Column values that will become column headers>
);

Important

For aggregate functions in the PIVOT operator, any null values in the value column are not considered during computation.

Let’s walk through an example to get a...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime