Тёмный

SQL Tutorial - PIVOT 

BeardedDev
Подписаться 11 тыс.
Просмотров 128 тыс.
50% 1

Опубликовано:

 

2 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 146   
@lanw8949
@lanw8949 2 года назад
Thanks a lot for explaining this so clearly! This is really helpful!!
@pabeader1941
@pabeader1941 4 года назад
Thank you for this video. I've watched many others about pivot and yours is the first one that finally made it make sense for me.
@BeardedDevData
@BeardedDevData 4 года назад
That’s great, glad I could help
@reneauk6956
@reneauk6956 4 года назад
Thank you so much for saying straight out how to use this. Referring to docs can be annoying when they're overly verbose.
@Bulgarian83
@Bulgarian83 Год назад
Much apreciated for this tutorial. It was very helpful.
@Maztergyl666
@Maztergyl666 4 года назад
0:25 "And it makes it easy to analoise data"
@houstonvanhoy7767
@houstonvanhoy7767 Год назад
Thank you, Professor Higgins. 🙂
@duco14
@duco14 5 лет назад
munth
@CaribouDataScience
@CaribouDataScience 2 года назад
Thanks so much. This is very helpfull.
@xxapoloxx
@xxapoloxx 3 года назад
My problem with pivot is that the "Spreading Elements" need to be hardcoded into the query, thus new elements go missing.
@harkiratsinghvasir7973
@harkiratsinghvasir7973 3 года назад
You got the solution?
@timjb3376
@timjb3376 3 года назад
any thought on doing a video on dynamic multi aggregate pivot tables? Whereby you can declare and pass parameters that define which values you want as the row definition and also specify the different aggregates and columns. IE Say you want a count of one field, a sum of a couple others and avg of a third but as the year progresses or topic changes you'd like to just change the input and not re hard code the whole multi aggregate pivot query.
@BeardedDevData
@BeardedDevData 3 года назад
It’s a good idea
@kaelhawker6021
@kaelhawker6021 3 года назад
Thank you for posting the syntax
@CaribouDataScience
@CaribouDataScience 2 года назад
What is wrong with my pivot? SELECT [United States], [Asia], [Canada] FROM ( SELECT (SELECT country FROM regions r WHERE r.region_id = e.region_id) as [country] FROM employees e ) AS Src PIVOT ( COUNT(country) FOR country IN ([Asia],[Canada],[United States]) ) AS Pivot
@BeardedDevData
@BeardedDevData 2 года назад
Changing the table alias from Pivot to something like Pvt should solve the error, it's because Pivot is a recognised keyword.
@CaribouDataScience
@CaribouDataScience 2 года назад
@@BeardedDevData Thanks for the suggestion. I changed it to Pvt , but am now getting this error : ERROR: syntax error at or near "[" LINE 1: SELECT [United States],
@BeardedDevData
@BeardedDevData 2 года назад
What database system are you using?
@CaribouDataScience
@CaribouDataScience 2 года назад
@@BeardedDevData Postgres
@BeardedDevData
@BeardedDevData 2 года назад
I'm not that familiar with postgres, I had a look and there is a crosstab function that looks similar to pivot, as an alternative you can take a look at this video, ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-xPMbgM8Eb2k.html
@MrKingoverall
@MrKingoverall 4 года назад
I LOVE YOU MAN !!!
@VanillaBean2023
@VanillaBean2023 3 года назад
Thanks for including the syntax in the description for this and your unpivot video. I copied and annotated both of them for myself for reference. Hopefully that will help my comprehension and retention of these statements and make me more comfortable with them.
@CaribouDataScience
@CaribouDataScience 2 года назад
Is there a reverse pivot? I.e. convert columns to rows?
@BeardedDevData
@BeardedDevData 2 года назад
You can check out my video on UNPIVOT - ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-0ERIr8UAK1s.html
@CaribouDataScience
@CaribouDataScience 2 года назад
@@BeardedDevData Thanks
@راجحالعمري-ت8ي
@راجحالعمري-ت8ي Год назад
thank you very much
@m4rt100
@m4rt100 4 года назад
hi brother! would you like to make video about dynamic pivot. thank you
@TheMihirpatel23
@TheMihirpatel23 3 года назад
Thank you so much, bro
@SepakanSudut
@SepakanSudut 4 года назад
holicow your explanation about pivot is the best. I've been struggling with pivot for years
@RobBurdine
@RobBurdine 3 месяца назад
Ditto all that. Truly the best format so a person can understand the terms as well as what is happening. As he builds the pivot table when he writes over the you can understand what is happening. He uses terms like the spreader. Describing what is going to spread out or fan out the columns of data that will be displayed. Good job .....really good job.
@BeardedDevData
@BeardedDevData 3 месяца назад
Thanks so much.
@wicked_cool7
@wicked_cool7 Месяц назад
I'm starting to think all the Microsoft documentation is written by AI xD. It's so overly complicated.
@medAmineRg
@medAmineRg 2 года назад
that was very helpfull thankyou broo
@PopCapMusicTrending
@PopCapMusicTrending Год назад
Hi I need help 😥I want to display the average in 2 decomal places but Idk how SELECT Student, [English], [Mathematics], [Science], [Programming], [History] FROM ( SELECT Student, Grades, Subject FROM Grade_Report) AS SourceTable PIVOT ( AVG (Grades) FOR Subject IN ([English], [Mathematics], [Science], [Programming], [History]) ) AS PivotTable
@BeardedDevData
@BeardedDevData Год назад
It's one of the downsides of PIVOT when working with decimals, you can't do the conversion within the PIVOT statement therefore you need to do this in the outer select, SELECT, Student, CAST([Mathematics] AS DECIMAL(6, 2)) AS [Mathematics] ...
@PopCapMusicTrending
@PopCapMusicTrending Год назад
@@BeardedDevData Thank you very much! I was able to figure it out after I posted this. I have the same idea. 👊🏽🍾
@kkpw12
@kkpw12 4 года назад
Thank you. It was very concise and comprehensive
@londonboy007uk
@londonboy007uk 5 лет назад
Thanks great , in your second example where you use Sales as pivot columns I like to use as all 365 days (Calender Year) for example ('01-Jan-2019') , 02-Jan-2019 ….31-Dec-2019any guidance's please as I don't want to declare all the columns names with valuesThanksAny example for Dynamic value where the column values comes from data rather than type each column name?Yes do we have any example for for Dynamic pivot table?
@shariqkhan5790
@shariqkhan5790 4 года назад
Great collection of videos. Thank you. It will be really helpfull if you could share tables or database used in the videos.
@mauricemosseri4959
@mauricemosseri4959 Год назад
Great video. Very helpful. what if you dont have elements names, can it be dynamic?
@BeardedDevData
@BeardedDevData Год назад
Hi Maurice, I have got a separate couple of videos on dynamic pivoting: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-SzYHiuSy0ZU.html, ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-n9d8WSEBzAY.html
@louiscarter6338
@louiscarter6338 6 лет назад
You should do a dynamic pivot table for customer Id’s 👍🏻 seeing as they can always change.
@jeremyheminger4133
@jeremyheminger4133 5 лет назад
I was just about to ask if this was possible
@londonboy007uk
@londonboy007uk 5 лет назад
Yes do we have any example for for Dynamic pivot table?
@houstonfirefox
@houstonfirefox Год назад
Here is one way to do it: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-BZ8ojeOkRUg.html
@saripkat
@saripkat 3 месяца назад
Possibly the best explanation on the internet. Thank you.
@BeardedDevData
@BeardedDevData 3 месяца назад
Thanks so much.
@HuyHuy-wy6vi
@HuyHuy-wy6vi 2 года назад
can u show me, how to sum by column =>> total of sum(1),sum(2),sum(3),....
@BeardedDevData
@BeardedDevData 2 года назад
Hi, you might find this video useful if you want to add totals, ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-YPZwnb1WX9I.html.
@roopaktj5863
@roopaktj5863 4 года назад
Great video and nice explanation. I was wondering if we could automatically generate the column names based on the distinct values in the original column. (Like how pivot works in pandas python).
@BeardedDevData
@BeardedDevData 4 года назад
That is one of the main limitations of the pivot syntax. You can build it up dynamically but it does make the query hard to read.
@roopaktj5863
@roopaktj5863 4 года назад
@@BeardedDevData Could you create a video on loops using SQL. Thanks in advance
@plc.automation
@plc.automation 3 года назад
no much advertisments
@kaizokumugiwara3876
@kaizokumugiwara3876 6 лет назад
Nice work there..
@BeardedDevData
@BeardedDevData 6 лет назад
Thanks for the positive feedback.
@kayk1388
@kayk1388 Год назад
There should be a Heart react button for this video. Thank you. ❤️
@BeardedDevData
@BeardedDevData Год назад
Thanks so much.
@margin-fades
@margin-fades 4 месяца назад
Understandable, clear, and concise. Thank you!
@darrylw99
@darrylw99 Год назад
What I can understand is where are you getting [Month] from?
@BeardedDevData
@BeardedDevData Год назад
Hi Darryl, I use DATENAME(MONTH, sales_date) to retrieve the month name, in the pivot query I just type them in.
@albertmusembi7318
@albertmusembi7318 2 года назад
If I have another table with customer name and customer ID and I want to Inner JOIN and display the customer name instead of the customer ID how do I go about it?
@albertmusembi7318
@albertmusembi7318 2 года назад
SELECT [CustomerName] -- (
@BeardedDevData
@BeardedDevData 2 года назад
That's very odd, your syntax looks correct, I have even tried it and it works, what RDBMS are you using?
@omviral3567
@omviral3567 4 года назад
video pivot table using mysql -->ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-N34zwKoxQOA.html
@0i0l0o
@0i0l0o 3 месяца назад
Thank you BD
@nationalhaulers4949
@nationalhaulers4949 2 года назад
you are the best !!
@3m_Ragab
@3m_Ragab 3 месяца назад
Thank you
@nikhilkulkarni20
@nikhilkulkarni20 2 года назад
can u please tell how can we add a row/ column total to the pivot table
@BeardedDevData
@BeardedDevData 2 года назад
Hi Nikhil, I will be posting a video on this over this week, is there specific types of columns/rows you are looking to add such as totals?
@sandeepshekhar1492
@sandeepshekhar1492 4 года назад
The way you explain is incredible. Thank You :)
@oz7075
@oz7075 Год назад
Hello. I'm watching this playlist because I've watched every single minute of the window functions, which I've loved so much. One question: do we have to hardcode the names of the new columns in the part as well as after the IN? I find it quite inefficient and clunky. No way to make it code dependent? I can imagine that we can use a SELECT statement after the IN, but what about the section? Thanks!
@BeardedDevData
@BeardedDevData Год назад
Hi, I have separate videos on how to create this dynamically, this is part 1: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-SzYHiuSy0ZU.html
@BeardedDevData
@BeardedDevData Год назад
This is part 2: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-n9d8WSEBzAY.html
@oz7075
@oz7075 Год назад
@@BeardedDevData Thanks. I've watched it and it's pretty clear. I'll stick to part 2 for simplicity. So, there's no way to have multiple aggregations other than using grouping by SELECT CASE statement or joining multiple PIVOT operations, right? Just asking in case they made improvements after the release of your videos. P.S: I've really loved this playlist, so much so, I've watched every single minute of them. Thank you!
@BeardedDevData
@BeardedDevData Год назад
Not that I'm aware of in all honesty.
@Han-ve8uh
@Han-ve8uh 3 года назад
What does the [] surrounding month and January,February,March do? I was testing on sqliteonline.com and removed all the [] and pivot still worked, but when the new columns were integers 1,2,3... removing the [] around original column name is fine but [1],[2],[3] without brackets like IN (1,2,3..) will break with "Incorrect syntax near '1'."
@BeardedDevData
@BeardedDevData 3 года назад
It actually does nothing, it's just something I do as a habit when working with PIVOT, when creating columns if they start with regular identifiers such as letters a-z then [] are not required, integers are considered irregular therefore [] are required, you can leave them out if you wish
@maihuongbui683
@maihuongbui683 2 года назад
thanks teacher
@wolframfoxhole5586
@wolframfoxhole5586 2 года назад
Thank you!
@HawkLad
@HawkLad 2 года назад
How do you have a grand total at the end of the pivot table rows?
@Gamachy
@Gamachy 3 года назад
Thank you so much. I really appreciate your job.
@elisonarimino5783
@elisonarimino5783 3 года назад
Thanks!
@shaitte80
@shaitte80 5 лет назад
i'am barely deaf!!
@nickarrigo5540
@nickarrigo5540 3 года назад
another excellent BD video production!
@jonhdoe3979
@jonhdoe3979 3 года назад
Thank you so much! Very clear explanation!
@michaloborzynski8111
@michaloborzynski8111 6 лет назад
Thanks BeardedDev for your videos, they are very useful and informative. Is there any way to get the database you use for your examples? Is it freely available?
@BeardedDevData
@BeardedDevData 6 лет назад
Hi, thanks for the positive feedback, it is not something that is available yet but something I'm looking to do in the future
@NoajmIsMyName
@NoajmIsMyName 4 года назад
@@BeardedDevData Please is it yet ready to share the Sales Database with us ... it will be very helpful to apply your examples after watching your video
@tushardeepsingh4976
@tushardeepsingh4976 3 года назад
nicely explained ..thanks
@kfcollection3471
@kfcollection3471 4 месяца назад
Perfect explanation
@BeardedDevData
@BeardedDevData 4 месяца назад
Thanks so much.
@marymartha5095
@marymartha5095 4 года назад
great video. thank you.
@deavea
@deavea 2 года назад
Absolutelly love your accent! Thank's for video
@BeardedDevData
@BeardedDevData 2 года назад
Thanks so much.
@ottojager4032
@ottojager4032 3 года назад
Thanks! Very clear.
@shaikzuhair8537
@shaikzuhair8537 3 года назад
Very good explained
@wicked_cool7
@wicked_cool7 Месяц назад
This was easy to understand. Thank you.
@Rundtj45
@Rundtj45 2 года назад
how to summarize monthly data in annual level
@BeardedDevData
@BeardedDevData 2 года назад
First you will need to create your derived table with a year column, month column and then amount column, remember you can sum in the derived table if your data is at day level for example. Once that's done then you can perform the pivot operation.
@Rundtj45
@Rundtj45 2 года назад
@@BeardedDevData Thanks Sir now i wanna to save that creating column I mean the summ of 12 month in current table ? what shoul I do?
@sk____1958
@sk____1958 3 года назад
that was a really great explanation
@kevinruder9652
@kevinruder9652 5 лет назад
How do you know hat the column one is equivalent to supplier ID 1 ? Is there no ordering required ?
@BeardedDevData
@BeardedDevData 5 лет назад
The order of the columns is specified in select.
@nataliesanchez987
@nataliesanchez987 4 года назад
Awesomegreat teacher
@GrannySemo
@GrannySemo 4 года назад
For the second example, the table displayed shows aggregated total sales_amount for each customer id over the months. Can you explain how the table is aggregated by the months?
@BeardedDevData
@BeardedDevData 4 года назад
Month is part of the original source query but is not used as part of the pivot in the second example. What we are doing is asking for Month in addition to Sales_Amount pivoted by Customer_Id. I think I need to put up some more examples regarding pivot.
@rogereastwood
@rogereastwood 4 года назад
@@BeardedDevData This was great but like you stated - yes, we need a few more examples of pivot where you can 'hammer home' the inner workings of it so folks like me can fully get it.
@joshsmart9178
@joshsmart9178 3 года назад
How do you do the opposite of pivot? If your data is in a column header i.e the first row/header of data, how do you make those contents populate one column, row by row, going down?
@BeardedDevData
@BeardedDevData 3 года назад
Take a look at UNPIVOT: m.ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-0ERIr8UAK1s.html
@joshsmart9178
@joshsmart9178 3 года назад
@@BeardedDevData thanks man!
@sqlservertutorialin3minute485
@sqlservertutorialin3minute485 3 года назад
Nice one
@karinshamama5591
@karinshamama5591 3 года назад
Thank you sir
@ameobiamicrobiological2366
@ameobiamicrobiological2366 4 года назад
Could you use max/min in this for varchar values? So far I've not been successful in doing that, Hench the reason I am here.
@BeardedDevData
@BeardedDevData 4 года назад
Yes, you have to use MIN or MAX when working with varchar. I’m actually planning a video on this next week but if you want to send me some more details I will take a look.
@llatyntseva
@llatyntseva 3 года назад
Thanks alot!
@EbereOyekwe
@EbereOyekwe Год назад
Great stuff!
@BeardedDevData
@BeardedDevData Год назад
Thanks so much.
@alfredsfutterkiste7534
@alfredsfutterkiste7534 3 года назад
Me: month BeardedDev: munth
@alfredsfutterkiste7534
@alfredsfutterkiste7534 3 года назад
But in all seriousness great idea to replace the syntax elements by actual values one by one to hammer home the concept. Well done.
@BeardedDevData
@BeardedDevData 3 года назад
Thank you for acknowledging that I pronounce the word correctly. The word, “Month” in British is pronounced muhnth, I am British, hence the pronunciation 👍.
@birukarbas5022
@birukarbas5022 4 года назад
what an info thanks
@skdonsingh
@skdonsingh 6 лет назад
Excellent video thanks
@rachaelp5967
@rachaelp5967 Год назад
this is the first video that worked for me, thank you!!
@BeardedDevData
@BeardedDevData Год назад
That's great to hear.
@learningchannel88
@learningchannel88 4 года назад
Let say you need to add several cases within the pivot. Can you do that?
@BeardedDevData
@BeardedDevData 4 года назад
Can you give me more detail on what you mean by cases
@houstonfirefox
@houstonfirefox 4 года назад
Nicely explained. Clear and concise with simple examples.
@BeardedDevData
@BeardedDevData 4 года назад
Thanks for the positive feedback, I’ve just posted two more videos on PIVOT if you want to check them out.
@MoCityMadMan
@MoCityMadMan 5 лет назад
Awesome, short and to the point. Was looking for something like this, not for the information genre you are using but something else. Works for me. Thumbs up and thanks!
@BeardedDevData
@BeardedDevData 5 лет назад
Thanks for the feedback, what was it that you were looking for?
@MoCityMadMan
@MoCityMadMan 5 лет назад
@@BeardedDevData I work with a technology platform that has multiple remote service agents on endpoints throughout the enterprise environment(the agents perform numerous functions on the endpoints depending on which ones are installed), there could be X number installed on one endpoint and a completely different number installed on another endpoint... and there could be/are numerous different versions of the agents installed on the endpoints. I just needed an example and apply the the 'months' as 'agent names' and 'sales revenue' as 'agent versions'. Cleared it up for me very easily! It is a more simpler solution than what I was working with in the past(CASE statements and lots of manipulation - never wrote a pivot in a sql query only Excel). Thanks again.
@kalburtdallof5031
@kalburtdallof5031 4 года назад
I have a table where I need to multiply within my aggregate function in the pivot but when I put the * withing the sum function to multiply the two together, I get an error. is there a different way to multiply within a pivot?
@kalburtdallof5031
@kalburtdallof5031 4 года назад
Pivot ( sum(CM_COST * Q_SHIP) FOR [Month] in ([January] , [February] , [March] ) AS Pvt This is what my pivot portion looks like
@christianspringer4363
@christianspringer4363 4 года назад
Thanks!
@darrylw99
@darrylw99 29 дней назад
Not much use if you need to know the columns. you surely need to get distinct values before you pivot. so its not dynamic
@BeardedDevData
@BeardedDevData 29 дней назад
Appreciate that, that's why I have separate videos on dynamic pivoting. You will find though even writing a basic query you will need to know the data.
@darrylw99
@darrylw99 29 дней назад
It wasnt a comment about your video. it was a comment about pivot itself..... thank you for the reply
@karinaigolnikov3946
@karinaigolnikov3946 5 лет назад
Any idea how to do this using Hive?
@BeardedDevData
@BeardedDevData 5 лет назад
I believe in Hive to PIVOT data you have to use the map function.
@alno1
@alno1 2 года назад
Very well explained!
@BeardedDevData
@BeardedDevData 2 года назад
Thanks so much.
@wexwexexort
@wexwexexort 3 года назад
Where is the accent from? It sounds so cool.
@BeardedDevData
@BeardedDevData 3 года назад
I’m from England.
@tonyp9616
@tonyp9616 6 лет назад
You got best videos in sql!
@BeardedDevData
@BeardedDevData 6 лет назад
Thanks so much
@fm5401
@fm5401 5 лет назад
say month good
Далее
T-SQL Tutorial - PIVOTing Made Easy
21:28
Просмотров 10 тыс.
SQL Tutorial - Window Functions - Ranking
17:32
Просмотров 30 тыс.
小路飞嫁祸姐姐搞破坏 #路飞#海贼王
00:45
Сколько стоит ПП?
00:57
Просмотров 143 тыс.
Pivoting Data with SQL
9:33
Просмотров 12 тыс.
Oracle SQL - PIVOT Clause
18:53
Просмотров 12 тыс.
Dynamically Pivot Data in SQL Server
13:50
Просмотров 84 тыс.
Solving SQL Query | Rows to Column in SQL
1:13:11
Просмотров 172 тыс.
SQL Unpivot and more
14:45
Просмотров 13 тыс.
How I use SQL as a Data Analyst
15:30
Просмотров 831 тыс.
SQL Pivot and Unpivot | Quick Tips Ep57
21:51
Просмотров 11 тыс.
小路飞嫁祸姐姐搞破坏 #路飞#海贼王
00:45