Questions tagged [pivot]

The PIVOT syntax converts row data into columnar data, and vice versa for the UNPIVOT syntax. The syntax is non-standard, and not all databases support the PIVOT syntax, but the functionality can be implemented, often using decision logic (CASE expressions, etc) and aggregate functions.

The PIVOT syntax converts row data into columnar data, and vice versa for the UNPIVOT syntax. The syntax is non-standard, and not all databases support the PIVOT syntax, but the functionality can be implemented, often using decision logic (CASE expressions, etc) and aggregate functions.

The PIVOT operation may also be referred to as cross-tabulation or .

Similar functionality also exists in data processing tools/packages such as (via pivot method), (via pivot_wider function), and (via unstack function).

Databases that support PIVOT/UNPIVOT syntax:

  • Oracle 11g+
  • SQL Server 2005+

While MySQL does not have a PIVOT function, this can be replicated using an aggregate function and either a CASE statement or IF()

PostgreSQL (true in 9.3 and below, at least) do not offer PIVOT, but do supply the crosstab function from the bundled tablefunc extension. This function may be used, albeit awkwardly, to achieve the same effect.

Reference:

11340 questions
608
votes
5 answers

How can I pivot a dataframe?

What is pivot? How do I pivot? Long format to wide format? I've seen a lot of questions that ask about pivot tables, even if they don't know it. It is virtually impossible to write a canonical question and answer that encompasses all aspects of…
piRSquared
  • 285,575
  • 57
  • 475
  • 624
487
votes
5 answers

Efficiently convert rows to columns in sql server

I'm looking for an efficient way to convert rows to columns in SQL server, I heard that PIVOT is not very fast, and I need to deal with lot of records. This is my…
tbag
  • 4,913
  • 3
  • 13
  • 8
383
votes
11 answers

How can I return pivot table output in MySQL?

If I have a MySQL table looking something like this: company_name action pagecount ------------------------------- Company A PRINT 3 Company A PRINT 2 Company A PRINT 3 Company B EMAIL Company B PRINT …
peku
  • 5,003
  • 3
  • 20
  • 15
328
votes
9 answers

Convert Rows to columns using 'Pivot' in SQL Server

I have read the stuff on MS pivot tables and I am still having problems getting this correct. I have a temp table that is being created, we will say that column 1 is a Store number, and column 2 is a week number and lastly column 3 is a total of…
Lynn
  • 3,534
  • 5
  • 18
  • 18
277
votes
7 answers

PostgreSQL Crosstab Query

Does any one know how to create crosstab queries in PostgreSQL? For example I have the following table: Section Status Count A Active 1 A Inactive 2 B Active 4 B Inactive 5 I would like the query to…
user151419
254
votes
9 answers

SQL Server dynamic PIVOT query?

I've been tasked with coming up with a means of translating the following data: date category amount 1/1/2012 ABC 1000.00 2/1/2012 DEF 500.00 2/1/2012 GHI 800.00 2/10/2012 DEF …
Sean Cunningham
  • 3,006
  • 5
  • 24
  • 35
178
votes
10 answers

TSQL Pivot without aggregate function

I have a table like…
ctrlShiftBryan
  • 27,092
  • 26
  • 73
  • 78
144
votes
9 answers

Simple way to transpose columns and rows in SQL?

How do I simply switch columns with rows in SQL? Is there any simple command to transpose? ie turn this result: Paul | John | Tim | Eric Red 1 5 1 3 Green 8 4 3 5 Blue 2 2 9 …
edezzie
  • 1,657
  • 4
  • 14
  • 12
131
votes
7 answers

SQL Server: Examples of PIVOTing String data

Trying to find some simple SQL Server PIVOT examples. Most of the examples that I have found involve counting or summing up numbers. I just want to pivot some string data. For example, I have a query returning the following. Action1 VIEW …
Tim Cochran
  • 1,834
  • 2
  • 15
  • 13
121
votes
11 answers

How can I combine multiple rows into a comma-delimited list in Oracle?

I have a simple query: select * from countries with the following results: country_name ------------ Albania Andorra Antigua ..... I would like to return the results in one row, so like this: Albania, Andorra, Antigua, ... Of course, I can write…
rics
  • 5,494
  • 5
  • 33
  • 42
113
votes
9 answers

Laravel, sync() - how to sync an array and also pass additional pivot fields?

Official Laravel documentation has this on sync() function: $user->roles()->sync( array( 1, 2, 3 ) ); You may also associate other pivot table values with the given IDs: $user->roles()->sync( array( 1 => array( 'expires' => true ) ) ); In the…
Томица Кораћ
  • 2,542
  • 7
  • 35
  • 57
103
votes
3 answers

Opposite of melt in python pandas

I cannot figure out how to do "reverse melt" using Pandas in python. This is my starting data label type value 0 x a 1 1 x b 2 2 x c 3 3 y a 4 4 y b 5 5 y c 6 6 z a …
Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
93
votes
1 answer

MySQL pivot row into dynamic number of columns

Lets say I have three different MySQL tables: Table products: id | name 1 Product A 2 Product B Table partners: id | name 1 Partner A 2 Partner B Table sales: partners_id | products_id 1 2 2 …
FeeJai
  • 1,175
  • 1
  • 8
  • 13
92
votes
7 answers

Understanding PIVOT function in T-SQL

I am very new to SQL. I have a table like this: ID TeamID UserID ElementID PhaseID Effort 1 1 1 3 5 6.74 2 1 1 3 6 8.25 3 1 1 4 1 2.23 4 1 1 4 5 6.8 5 1 1 4 6 1.5 And I was told to get data like…
Web-E
  • 1,169
  • 2
  • 12
  • 17
90
votes
10 answers

How to pivot Spark DataFrame?

I am starting to use Spark DataFrames and I need to be able to pivot the data to create multiple columns out of 1 column with multiple rows. There is built in functionality for that in Scalding and I believe in Pandas in Python, but I can't find…
J Calbreath
  • 2,665
  • 4
  • 22
  • 31
1
2 3
99 100