SQL Server // Do you want to lean how to pivot data in 5 minute?
Hi friends, What do you think about learning more about pivoting ? It is usually not considered a simple topic but it really is! Go! So, what is the pivoting operation? The pivoting operation consists in transforming the data coming from rows of a table into data grouped on several columns . We can consider the pivoting operation as an operation consisting of these three phases carried out in order: 1. Grouping (we want to have only one row for each distinct element on the rows. It is equivalent to a GROUP BY operation) 2. Spreading (we want to have n columns in which to box the values that we will then aggregate. It is equivalent to a CASE) 3. Aggregating (we aggregate the values on the columns for example with the SUM function) For example, we could pivot data in this way: SELECT SUM ( CASE WHEN BR . CODE = 'NIKE' THEN CR . QTA END ) AS [QTY_FOR_BRAND_WITH_CODE_NIKE] , SUM ( CASE WHEN BR . CODE = 'SNEAKERS' THEN CR ...