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
21
votes
6 answers

Pivot for redshift database

I know this question has been asked before but any of the answers were not able to help me to meet my desired requirements. So asking the question in new thread In redshift how can use pivot the data into a form of one row per each unique dimension…
ankitkhanduri
  • 315
  • 1
  • 2
  • 9
21
votes
2 answers

T-SQL Pivot? Possibility of creating table columns from row values

Is it actually possible to rotate a T-SQL (2005) so that (for the sake of argument) the values of the first column's rows become the titles of the output table's columns? I realise this is not really what PIVOT is for, but it's what I need - the…
Matt W
  • 11,753
  • 25
  • 118
  • 215
21
votes
11 answers

Dynamic Pivot in Oracle's SQL

... pivot (sum(A) for B in (X)) Now B is of datatype varchar2 and X is a string of varchar2 values separated by commas. Values for X are select distinct values from a column(say CL) of same table. This way pivot query was working. But the…
prabhakar
  • 385
  • 1
  • 2
  • 8
21
votes
3 answers

Pivot on Multiple Columns using Tablefunc

Has anyone used tablefunc to pivot on multiple variables as opposed to only using row name? The documentation notes: The "extra" columns are expected to be the same for all rows with the same row_name value. I'm not sure how to do this without…
ideamotor
  • 856
  • 1
  • 7
  • 23
20
votes
2 answers

SQL PIVOT SELECT FROM LIST (IN SELECT)

Is it possible to do a PIVOT and select list from a table, instead of using single values? Like this (incorrect syntax error): SELECT * FROM ( SELECT RepID, MilestoneID, ResultID FROM RM ) AS src PIVOT ( MAX(ResultID) FOR MilestoneID IN …
live-love
  • 48,840
  • 22
  • 240
  • 204
20
votes
4 answers

Oracle SQL pivot query

I have data in a table as seen below: MONTH VALUE 1 100 2 200 3 300 4 400 5 500 6 600 I want to write a SQL query so that result is given as below: MONTH_JAN MONTH_FEB MONTH_MAR MONTH_APR MONTH_MAY MONTH_JUN 100 200 …
code990
  • 551
  • 2
  • 5
  • 8
20
votes
1 answer

Use row values as columns in PostgreSQL

I have the following brands table with total sales per month as a result of a previous query: id | date | total -----+----------+------ 123 | Apr-2012 | 100 123 | Mar-2012 | 150 123 | Jan-2012 | 500 987 | Apr-2012 | 5 987 | Mar-2012 |…
Jamie
  • 651
  • 2
  • 6
  • 10
20
votes
4 answers

Can SQL Server Pivot without knowing the resulting column names?

I have a table that looks like this: Month Site Val 2009-12 Microsoft 10 2009-11 Microsoft 12 2009-10 Microsoft 13 2009-12 Google 20 2009-11 Google 21 2009-10 Google 22 And I…
Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
20
votes
3 answers

How to convert Rows to Columns in Oracle?

I have a table in this form (this is just the partial view, the table contains more than 100 columns). LOAN NUMBER DOCUMENT_TYPE DOCUMENT_ID 992452533663 Voters ID XPD0355636 992452533663 Pan card …
MontyPython
  • 2,906
  • 11
  • 37
  • 58
20
votes
1 answer

How to transpose mysql table rows into columns

Here is what my current mysql table looks like: PunchID EmpID PunchEvent PunchDateTime 1 0456 clockin 5/14/2013 8:36:26 AM 48 0456 breakout 5/14/2013 12:01:29 PM 53 0456 breakin 5/14/2013 12:28:31 PM 54 …
dmikester1
  • 1,374
  • 11
  • 55
  • 113
20
votes
3 answers

Setting column values as column names in the SQL query result

I wanted to read a table which has values which will be the column names of the sql query result. For example, I have table1 as .. id col1 col2 ---------------------- 0 name ax 0 name2 bx 0 name3 cx 1 name dx 1 …
ravi
  • 1,707
  • 4
  • 29
  • 44
19
votes
3 answers

How to remove multilevel index in pandas pivot table

I have a dataframe as given: df = {'TYPE' : pd.Series(['Advisory','Advisory1','Advisory2','Advisory3']), 'CNTRY' : pd.Series(['IND','FRN','IND','FRN']), 'VALUE' : pd.Series([1., 2., 3., 4.])} df = pd.DataFrame(df) df =…
Shivpe_R
  • 1,022
  • 2
  • 20
  • 31
19
votes
1 answer

Pivot table returns multiple rows with NULL, results should be grouped on one row

I have the table below which I am looking to pivot so that the descriptions in column 1 become column headers in the new pivot. Nominal Group | GrpID | Description | Value | CustomerID …
Stu412
  • 251
  • 1
  • 3
  • 11
19
votes
2 answers

how to unstack (or pivot?) in pandas

I have a dataframe that looks like the following: import pandas as pd datelisttemp = pd.date_range('1/1/2014', periods=3, freq='D') s = list(datelisttemp)*3 s.sort() df =…
codingknob
  • 11,108
  • 25
  • 89
  • 126
19
votes
1 answer

Convert Access TRANSFORM/PIVOT query to SQL Server

TRANSFORM Avg(CASE WHEN [temp].[sumUnits] > 0 THEN [temp].[SumAvgRent] / [temp].[sumUnits] ELSE 0 END) AS Expr1 SELECT [temp].[Description] FROM [temp] GROUP BY [temp].[Description] PIVOT…
Vipin Jain
  • 1,382
  • 1
  • 10
  • 19