PIVOT operator |
||||||||||||||||||||||||
Let's look at this task. Example 1. Determine the number of models of each type of product for each maker from table Product.
I hope that the comments to the code clear enough to write a PIVOT operator without cribs. Let’s try it. Example 2. Calculate the average price on laptops depending on screen size. Task is elementary and solved by the grouping:
Unlike pivot tables, in PIVOT operator, explicit list of columns is required to output. This is a serious restriction, because need to know what kind of data, and therefore we can apply this operator, as a rule, only to lookup tables (or rather, to the data taken from lookup tables). If the examples studied seem insufficiently to understand and use without difficulties this operator, I will come back to it when I will think up nontrivial examples where PIVOT operator usage allows simplifying a code essentially. I wrote this opus to help those who having trouble with PIVOT operator. I can agree that in relational language SQL(Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.SQL it looks a foreign matter. Indeed, otherwise it cannot be due to rotation (transposition) of the table is not a relational operation, but operation with multidimensional data structures. |