From the course: Hands-On Advanced SQL Server: Strategies and Techniques
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Dynamic Pivot tables - SQL Server Tutorial
From the course: Hands-On Advanced SQL Server: Strategies and Techniques
Dynamic Pivot tables
- [Instructor] In the last video, you created a dynamic pivot table that showed how many movies belong to each genre. And you did that without hard coding the genre names. Now that's huge because it means your query now adapts as new genres are added, or existing ones are removed from the database. Now, I'd just like to reinforce the key steps and show how to customize your dynamic pivot even further. So in front of us is that pivot table that we created earlier. I just want to step through each section and make sure that we fully understand every part of it. Now, starting with the line up top, we're just declaring our columns variable and assigning it a data type of NVARCHAR MAX. Below it, we're using this query to build our string of genre names. We're using the quote name function to wrap each genre in square brackets, and then we're surrounding that with the string aggregate function to join them into a single string separated by commas. Now, the next line below, we're declaring…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.