Podcast Lookup

🛢️ Sql Pivot

Programmers

  • February 17, 2023
  • 5 min
Episode page
About this episodeIn this episode of programmers quickie podcast, we continue our exploration of SQL pivot and dive deeper into the syntax behind this…
In this episode of programmers quickie podcast, we continue our exploration of SQL pivot and dive deeper into the syntax behind this powerful data analysis tool. If you're looking to take your pivot table skills to the next level, this episode is for you!

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

🛢️ Sql Pivot

00:00—
  1. When we create a reporting table then it has columns and laws after all, it's a table. And in these columns and rows, which would you prefer the columns and rows to be? Would you prefer the columns to be the list of metrics that say that we have 20 metrics like some of users? Some of the quest, some of Click, some of the total revenue etc. Etc.

  2. And this could be either The Columns of the walls and the other dimension would be the mass of some of clicks per month. January February, March April, The sum of total revenue bill, January February, so I would prefer the columns to be the man's January February. March April, May, June, Etc. And I would prefer then the roads to be the Matrix, because then in this way, I can add more and more Matrix to the holes, what the math, stay constants and in a single screen and it would this would fit my screen now. Well, such that if I have 100 Matrix, I could scroll Look at all the metrics and I would simply see them all one by one by month.

  3. This just makes more sense in. This is how most reporting oh big to it. So in SQL, the thing is that if we are on a standard SQL with the go by, in order to get these totals of the Matrix, then when we use the go by, then what we would get is we would do a goal by month and then A sum of all the Matrix and we would get a wall for each month and condoms would be the Matrix. Why would it be like this?

  4. Because the select statement was the select for the Matrix and each thing that we put in the select is resulting with another column. This is how select works. And the goodbye would result in different roles. This is where the pivot could serve as time we could do this manually in SQL. But of course you could say that your reporting system is able to transform it but we also want to directly get this result so that we can iterate upon the scale and see the exact results just like they're on the reports we want to Wait on the scale and see the exact same results as they would appear on the report with Columns, of Demands and the Wolves of The Matrix. So the pivot statements will help us with this. The pivot statement comes in after the form, so we have select list of columns form which table and then pivot, the pivot would receive a few parameters. So again we have select multiple columns, that we put to the select then form which table and

  5. then pivot in the pivot we say just pivot some of the sales for month in January February. So the in but the last part would limit the number of months that we want. Basically we just supply to the pivot, the aggregate function and List of columns that we want to operate the Aggregate and that's it, we would get the month as the columns it would transform. It will get a month as the columns because we use the pivot.

  6. So, again, it's a very nice scale syntactic sugar. But we do a select form, a list of columns, then form which table. And then we do a pivoting based on columns, And we say we want to pave work by the sum of the sales amount for months in January, February or March a point and then we could select another and columns that we want to Pivot for. Not only the sum of the system, and also the sum of the clicks, the sum of the number of users, the sum of the number of requests, the sum of the number of conversions, the average and so forth.

  7. So this is the the pivot. The concept is just to use it after the form. After we use a select for multiple columns we say form, which table, and he say in the past we said go buy something right now. Instead of copper, we use pivot on this, a pivot by these problems, but we also need to supply to their pivot which other variation. Oh, sandwich summaries with want to. So this was the how to use the pivot in order to get aggregation based on columns.

  8. Oh, sandwich summaries with want to. So this was the how to use the pivot in order to get aggregation based on columns.