Wednesday, 21 August 2013

Pivot my table

Let's take a look to a simple example of how create a pivot table, useful when you work with reports and summary statistics.

The table employee contains 10 records with the employees details (Name, salary, role and year of reference)


SELECT * FROM EMPLOYEE
















With the Pivot operator we can easily generate a crosstab.

SELECT  * 
   FROM (SELECT YEAR,
                        EMP_SALARY,
                        EMP_ROLE 
               FROM EMPLOYEE)
  PIVOT (SUM(EMP_SALARY) 
              FOR EMP_ROLE IN  ('Manager' AS MANAGER,
                                           'Seller' AS SELLER ,
                                           'Cashier' AS CASHIER)

             )        

Here the result below







Have a nice Pivot

No comments:

Post a Comment