SQL Tutorial - UNPIVOT

  Рет қаралды 8,485

BeardedDev

BeardedDev

Күн бұрын

Пікірлер: 15
@QuietLumina
@QuietLumina Жыл бұрын
Still helping people 2 years later. I appreciate this so much!! Thank you!
@BeardedDevData
@BeardedDevData Жыл бұрын
Glad I could help!
@colinmacguire
@colinmacguire Жыл бұрын
Very useful and clear. Helped me solve my challenge. Thank you!
@yueliang8012
@yueliang8012 2 жыл бұрын
Clear and well explained! Thanks for the turoial!!
@BeardedDevData
@BeardedDevData 2 жыл бұрын
No problem 👍
@user-vy1ex9xn5e
@user-vy1ex9xn5e Жыл бұрын
Thanks for the turoial
@malcorub
@malcorub 2 жыл бұрын
Thanks beardo!
@garypolinsky4888
@garypolinsky4888 Жыл бұрын
Thank you!
@ck36121
@ck36121 3 жыл бұрын
How to deal with NULL values in unpivot? I have some null values in value column? thx.
@BeardedDevData
@BeardedDevData 3 жыл бұрын
I will make a video on this but in short you need to replace NULLs with a value, make sure it’s something that doesn’t appear in the data then perform the UNPIVOT then replace vales with NULLs using NULLIF.
@allmazd
@allmazd Жыл бұрын
So, there is another technique to obtaine same unpivot operation by using cross apply + value. Can you make a video that describes this approach in details, thank you in advance.
@BeardedDevData
@BeardedDevData Жыл бұрын
That's a great idea, I'm taking the holidays off but will be uploading regularly early next year.
@Han-ve8uh
@Han-ve8uh 3 жыл бұрын
1. In your PIVOT video kzbin.info/www/bejne/pavcZGSXf9WKY9E&ab_channel=BeardedDev you mentioned an aggregation function. Why doesn't UNPIVOT also need/allow an aggregate? 2. Is it something to do with the aggregation after pivot is an irreversible operation and data --> PIVOT (with aggregate used) --> UNPIVOT --> does not recover data? Practically why would people want to unpivot if it does not do aggregation and thus does not generate new information? (any reasons beyond just presentation?) 3. How can we show the null cells (in the pivot table) after UNPIVOT? 4. Is there a link to the video about why all TINYINT when unpivot?
@BeardedDevData
@BeardedDevData 3 жыл бұрын
1. UNPIVOT is a process of moving data from columns to rows, the aim is really to get the data in to a useable tabular format therefore an aggregation is not applicable, once the data is in the required format then aggregation can take place e.g. imagine if I have columns customerid and then one column for each month of the year that stores sales figures, if I want to get a total for each month of the year then I can perform an aggregation on each column, if I want to get the total I need to use an expression January + February + March etc, however if I move the data from columns to rows and I have columns customerid, month and sales it is now much easier to work with and perform aggregations 2. A lot of people mistake UNPIVOT as the reverse of PIVOT, the naming doesn't really help, think about when performing an aggregation e.g. if we have a table that stores customerid and sales, we then perform a SUM on sales, now all we have is a sales total, if we don't have the original data how can we reverse this process, we don't know the sales amount by customer, all we have is a total. As mentioned above we use UNPIVOT typically where our data is in a format that makes the data difficult to work with, it's more than just presentation, a good example is when we are supplied with spreadsheets that are in a matrix/pivot format, these are great for visualisations but not so good in database, we want to store data efficiently and perform calculations easily 3. To show NULLs is a two step process, as the UNPIVOT operation will remove them you need to add a placeholder, e.g. ISNULL(Value, 99) before UNPIVOT - make sure this value doesn't mean anything in your data otherwise it can be confusing, then change the placeholder value to NULL e.g. NULLIF(Value, 99) 4. TINYINT is a data type I used in the video, simply because the values stored could only be in the range 0 - 5, you will need to use the correct data type for your data
@Han-ve8uh
@Han-ve8uh 3 жыл бұрын
@@BeardedDevData Thanks for the explanation, now I can see how difficult it is to sum across a row, where we have to write col1+col2+..., instead of SUM(col) down a column. Nice hack to make NULL remain.
T-SQL Tutorial - PIVOTing Made Easy
21:28
BeardedDev
Рет қаралды 10 М.
SQL Tutorial - Window Functions (Follow Along)
23:16
BeardedDev
Рет қаралды 6 М.
Самое неинтересное видео
00:32
Miracle
Рет қаралды 2,5 МЛН
I Took a LUNCHBAR OFF A Poster 🤯 #shorts
00:17
Wian
Рет қаралды 17 МЛН
SQL Unpivot Simplified (Turn Columns into Rows)
8:24
Ryan & Matt Data Science
Рет қаралды 504
SQL Unpivot and more
14:45
James Oliver
Рет қаралды 12 М.
SQL | Unpivot |Convert data from columns into Rows | Advanced
7:41
Learn at Knowstar
Рет қаралды 62 М.
T-SQL Tutorial - PIVOT without using PIVOT
15:43
BeardedDev
Рет қаралды 5 М.
Advanced SQL Tutorial | Temp Tables
10:19
Alex The Analyst
Рет қаралды 253 М.
SQL Tutorial - How to create a Dynamic Pivot in SQL Part 2
11:16
BeardedDev
Рет қаралды 3,4 М.
SQL Pivot and Unpivot | Quick Tips Ep57
21:51
Joey Blue
Рет қаралды 11 М.
MS SQL Server.  Работа с несколькими таблицами.  10.  Оператор Pivot
15:34
Юрий Алексеевич Кремень
Рет қаралды 4,3 М.
SQL Tutorial - PIVOT
13:11
BeardedDev
Рет қаралды 127 М.
Самое неинтересное видео
00:32
Miracle
Рет қаралды 2,5 МЛН