How to Pivot Your Data in T-SQL for SQL Server - Static and Dynamic Column Examples
Вставка
- Опубліковано 9 гру 2021
- In this episode, we're going to look at how to pivot your data in SQL Server T-SQL queries. Pivoting is a very useful way of transforming data, and one of the most effective ways to show summary data. First I'll show you how to do a pivot query with static columns, and then I'll show you a more advanced method that will allow you to have a dynamic display of an unknown number of columns. This tutorial will also introduce you to the STRING_AGG function, which simplifies the creation of a delimited list in SQL Server 2017+ and Azure SQL environments.
For developers looking for a new role, check out and sign up:
www.toptal.com/qKaO2b/worlds-...
Needing to hire technical resources for your project? Get the best:
www.toptal.com/qKaO2b/worlds-...
Want to get access to premium content made just for you and have a chat with me? Find me on Patreon :
/ mackenziedataengineering
Demo of my BZ RDP Cloaker:
www.patreon.com/posts/how-to-...
Want to work with me 1:1? Contact me today and book your free 20 min consultation!
Contact form you can find at www.mackenziemackenzie.com/
Follow me on social media:
/ mackenziedataanalytics
/ seamacke
/ seamacke
/ seamacke
/ psmackenzie
Get Microsoft Office including Access:
click.linksynergy.com/fs-bin/...
Got a UA-cam Channel? I use TubeBuddy, it is awesome. Give it a try:
www.tubebuddy.com/seanmackenz...
#sqlserver #pivot #tsql
• How to Pivot Your Data... - Наука та технологія
Wow, easier, simple. Thanks!
Thank you so much, first video that helped me solve my problem with pivoting :)
Glad it helped!
Such a great video Sean! I have saved this in my SQL favs to reference as and when needed! This actually solves an issue I have now. I have followed along and so far so good. I am at the point where i need to creat my own version of the @myquery string. One question are you using the + as a line break as you would when line breaking a string in VBA? Thx again Sean.
and BOOM... Got it working :). I originally tried using too many CTEs and got myself in a muddle. Switched to sub queries and it worked a treat. Thx Sean!
Awesome that you got it working! In this one I believe I'm using the + like you would use & in VBA: to concatenate the string. Thanks for bookmarking - you know what's funny is that sometimes I watch my own video because I forgot how I did something before.. lol!
Thank you Sean !
You're welcome! Glad it was helpful.
Hi, I have a question, the data which is generated using aggregation in pivot, can we modify the data into another format. for example, if we have 0's and 1's in the data i need to replace them with desired format like 0 to NO, 1 to Yes . Can you please help
One easy solution is to make a query that uses your cross tab as a source. For that column just use IIF in your query for that column. Ie. if it is 0 then No else Yes. See my video on iif