SQL Server - What is Pivot table ?? - Asked By rajesh kumar on 17-Feb-12 02:25 AM

Can any one explain me about PIVOT table ??
Somesh Yadav replied to rajesh kumar on 17-Feb-12 02:27 AM

A pivot table is a frequently used method of summarizing and displaying especially report data by means of grouping and aggregating values.
Pivot tables are easily created by office users using Microsoft Excel or MS Access.
Since pivot table enables report builders and BI (Business Intelligence) specialists empower their presentation of reports and increase the visibility and understandability of mined data, pivot tables are common and preferred widely.

Pivot tables display data in tabular form. The pivot table formatting is not different than a tabular report formatting.
But the table columns are formed by the report data itself.

Microsoft SQL Server has introduced the PIVOT and UNPIVOT commands as enhancements to t-sql with the release of MS SQL Server 2005.

In MS SQL Server 2008, we can still use the PIVOT command and UNPIVOT command to build and use pivot tables in sql.
T-SQL Pivot and Unpivot statements will transform and rotate a tabular data into an other table value data in sql .
Since Pivot / Unpivot are SQL2005 t-sql enhancements, databases which you want to execute pivot and unpivot commands should be at least at compatibility level 90 (SQL2005) or 100 (SQL2008).

T-SQL Pivot Syntax

1 SELECT
2   [non-pivoted column], -- optional
3   [additional non-pivoted columns], -- optional
4   [first pivoted column],-- optional
5  [additional pivoted columns]
6 FROM (
7  SELECT query producing sql data for pivot
8  -- select pivot columns as dimensions and
9  -- value columns as measures from sql tables
10 ) AS TableAlias
11 PIVOT
12 (
13  <aggregation function>(column for aggregation or measure column) -- MIN,MAX,SUM,etc
14  FOR [<column name containing values for pivot table columns>]
15  IN (
16  [first pivoted column], ..., [last pivoted column]
17  )
18 ) AS PivotTableAlias
19 ORDER BY clause -- optional

Example SQL server 2005:

1 create table order_rep (iteNag varchar(10), mnth int, year int, ordermade int)
2  
3 insert into order_rep values('Shiva',1,2009, 5)
4 insert into order_rep values('Shiva',2,2009, 6)
5 insert into order_rep values('Shiva',1,2009, 10)
6 insert into order_rep values('Shiva',4,2009, 5)
7 insert into order_rep values('Shiva',5,2009, 7)
8 insert into order_rep values('Shiva',2,2009, 5)
9 insert into order_rep values('Shiva',1,2009, 4)
10 insert into order_rep values('Shiva',6,2009, 15)
11 insert into order_rep values('Shiva',8,2009, 8 )
12 insert into order_rep values('Shiva',3,2010, 5)
13 insert into order_rep values('Shiva',5,2010, 7)
14 insert into order_rep values('Shiva',12,2010, 5)
15 insert into order_rep values('Shiva',11,2010, 4)
16 insert into order_rep values('Shiva',1,2010, 7)
17 insert into order_rep values('Shiva',5,2010, 5)
18  
19 insert into order_rep values('Soft',2,2009, 6)
20 insert into order_rep values('Soft',4,2009, 7)
21 insert into order_rep values('Soft',2,2009, 4)
22 insert into order_rep values('Soft',3,2009, 5)
23 insert into order_rep values('Soft',5,2009, 7)
24 insert into order_rep values('Soft',12,2009, 12)
25 insert into order_rep values('Soft',11,2009, 4)
26 insert into order_rep values('Soft',1,2009, 9)
27 insert into order_rep values('Soft',5,2009, 4)
28 insert into order_rep values('Soft',3,2009, 5)
29 insert into order_rep values('Soft',4,2010, 7)
30 insert into order_rep values('Soft',1,2010, 1)
31 insert into order_rep values('Soft',4,2010, 4)
32 insert into order_rep values('Soft',2,2010, 9)
33 insert into order_rep values('Soft',5,2010, 4)
34  
35 insert into order_rep values('Nag',1,2009, 5)
36 insert into order_rep values('Nag',3,2009, 6)
37 insert into order_rep values('Nag',5,2009, 8 )
38 insert into order_rep values('Nag',12,2009, 23)
39 insert into order_rep values('Nag',9,2009, 45)
40 insert into order_rep values('Nag',5,2009, 3)
41 insert into order_rep values('Nag',1,2009, 5)
42 insert into order_rep values('Nag',4,2009, 3)
43 insert into order_rep values('Nag',3,2009, 9)
44 insert into order_rep values('Nag',3,2010, 5)
45 insert into order_rep values('Nag',5,2010, 7)
46 insert into order_rep values('Nag',12,2010, 12)
47 insert into order_rep values('Nag',11,2010, 4)
48 insert into order_rep values('Nag',1,2010, 9)
49 insert into order_rep values('Nag',5,2010, 4)

Now the script using Pivot table is:

1 SELECT *
2 FROM (
3 SELECT IteNag
4     , cast(Year as varchar(4)) + ' ' + CONVERT(varchar(3), dateadd(m, mnth, -1), 107) as MnthName
5     , OrderMade
6 FROM Order_rep
7 ) P
8 PIVOT (
9 SUM(OrderMade)
10 FOR MnthName IN ([2009 Apr], [2009 May], [2009 Jun], [2009 Jul], [2009 Aug])
11 ) AS PVT
12 drop table order_rep

Output:

http://shivasoft.in/blog/wp-content/uploads/2010/09/SQL-Server-Pivot-Table.png

SQL Server Pivot Table Output

Web Star replied to rajesh kumar on 17-Feb-12 02:31 AM
Pivot is feature of sql server so you can using pivot/unpivot get the data from row to column and vice-versa.
See this is good example of pivot here
http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/ 
kalpana aparnathi replied to rajesh kumar on 17-Feb-12 04:50 AM
hi,

Pivot table:

pivot table is a data summarization tool found in data visualization programs such as http://en.wikipedia.org/wiki/Spreadsheet or http://en.wikipedia.org/wiki/Business_intelligence software. Among other functions, pivot-table tools can automatically sort, count, total or give the average of the data stored in one table or spreadsheet. It displays the results in a second table (called a "pivot table") showing the summarized data. Pivot tables are also useful for quickly creating unweighted http://en.wikipedia.org/wiki/Cross_tabulation. The user sets up and changes the summary's structure by http://en.wikipedia.org/wiki/Drag_and_drop fields graphically. This "rotation" or pivoting of the summary table gives the concept its name. The term pivot table is a generic phrase used by multiple vendors. However, http://en.wikipedia.org/wiki/Microsoft_Corporation has http://en.wikipedia.org/wiki/Trademark the specific form PivotTable

Read more:http://en.wikipedia.org/wiki/Pivot_table


Regards,
Suchit shah replied to rajesh kumar on 17-Feb-12 06:14 AM
What Is  Pivot ?

You can use the PIVOT and UNPIVOT relational operators to change a table-valued expression into another table. PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, and performs aggregations where they are required on any remaining column values that are wanted in the final output. UNPIVOT performs the opposite operation to PIVOT by rotating columns of a table-valued expression into column values.

Where we use the Pivot ?
we use it on table valued expression in to another. it rotates table value expression using unique value in to multiple column in out and perform aggeration


Syntax

The syntax for PIVOT provides is simpler and more readable than the syntax that may otherwise be specified in a complex series of SELECT...CASE statements.

The following is annotated syntax for PIVOT.

SELECT <non-pivoted column>,

    [first pivoted column] AS <column name>,

    [second pivoted column] AS <column name>,

    ...

    [last pivoted column] AS <column name>

FROM

    (<SELECT query that produces the data>)

    AS <alias for the source query>

PIVOT

(

    <aggregation function>(<column being aggregated>)

FOR

[<column that contains the values that will become column headers>]

    IN ( [first pivoted column], [second pivoted column],

    ... [last pivoted column])

) AS <alias for the pivot table>

<optional ORDER BY clause>;


How to use the Pivot ?

Example

-- Pivot table with one row and five columns
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost
    FROM Production.Product) AS SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) AS PivotTable;

DL M replied to rajesh kumar on 18-Feb-12 04:04 AM
Hi..

A pivot table is a program tool that allows you to reorganize and summarize selected columns and rows of data in a spreadsheet or database table to obtain a desired report. A pivot table doesn't actually change the spreadsheet or database itself. In database lingo, to pivot is to turn the data (see slice and dice) to view it from different perspectives.

 A pivot table is especially useful with large amounts of data. For example, a store owner might list monthly sales totals for a large number of merchandise items in an Excel spreadsheet. If the owner wanted to know which items sold better in a particular financial quarter, it would be very time-consuming for her to look through pages and pages of figures to find the information. A pivot table would allow the owner to quickly reorganize the data and create a summary for each item for the quarter in question.

Pivot tables display data in tabular form. The pivot table formatting is not different than a tabular report formatting but the table columns are formed by the report data itself. I mean as a pivot table example, your report creator can build a report with years and months in the left side of the table, the main product lines are displayed as columns, and total sales of each product line in the related year and month is displayed in the cell content.http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/ with example.