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
27
votes
2 answers

How to pivot text columns in SQL Server?

I have a table like this in my database (SQL Server 2008) ID Type Desc -------------------------------- C-0 Assets No damage C-0 Environment No impact C-0 People No injury or health effect C-0 Reputation No…
Dinesh
  • 2,026
  • 7
  • 38
  • 60
26
votes
7 answers

Calculate difference from previous item with LINQ

I'm trying to prepare data for a graph using LINQ. The problem that i cant solve is how to calculate the "difference to previous. the result I expect is ID= 1, Date= Now, DiffToPrev= 0; ID= 1, Date= Now+1, DiffToPrev= 3; ID= 1, Date= Now+2,…
Marty
  • 3,485
  • 8
  • 38
  • 69
26
votes
6 answers

How to pivot rows into columns (custom pivoting)

I have a Sql Database table similar to the following: Day Period Subject Mon 1 Ch Mon 2 Ph Mon 3 Mth Mon 4 CS Mon 5 Lab1 Mon 6 Lab2 Mon 7 Lab3 Tue 1 Ph Tue 2 …
Babu
  • 263
  • 1
  • 3
  • 4
26
votes
2 answers

I need to know how to create a crosstab query

I need help creating the below results. I thought of a sql pivot but I don't know how to use it. Looked at a few examples and cannot come up with a solution. Any other ideas on how to accomplish this is also welcome. Status columns must be…
Sam
  • 265
  • 1
  • 3
  • 9
25
votes
6 answers

In SQL Server how to Pivot for multiple columns

This is my sample table, I want to pivot the category column and get the sales, stock and target as rows I want the sample output in this form as shown in the below wherein the categories are in place of columns and columns in place of row
Saad
  • 259
  • 1
  • 3
  • 3
25
votes
6 answers

Reshaping/Pivoting data in Spark RDD and/or Spark DataFrames

I have some data in the following format (either RDD or Spark DataFrame): from pyspark.sql import SQLContext sqlContext = SQLContext(sc) rdd = sc.parallelize([('X01',41,'US',3), ('X01',41,'UK',1), …
Jason
  • 2,834
  • 6
  • 31
  • 35
25
votes
3 answers

SQL Server Pivot Table with multiple column aggregates

I've got a table: create table mytransactions(country varchar(30), totalcount int, numericmonth int, chardate char(20), totalamount money) The table has these records: insert into mytransactions(country, totalcount, numericmonth, chardate,…
codingguy3000
  • 2,695
  • 15
  • 46
  • 74
24
votes
3 answers

Android setting pivot point for scale animation

I am trying to scale views to a certain size but can't quite understand how pivoting works. Say I want to scale the view upwards only. What value should the "pivotY" hold? In XML, it is a percentage. How is it when applying pivot point…
TalMihr
  • 1,528
  • 1
  • 15
  • 31
23
votes
4 answers

pivot_longer into multiple columns

I am trying to use pivot_longer. However, I am not sure how to use names_sep or names_pattern to solve this. dat <- tribble( ~group, ~BP, ~HS, ~BB, ~lowerBP, ~upperBP, ~lowerHS, ~upperHS, ~lowerBB, ~upperBB, "1", 0.51, 0.15, 0.05, …
Droc
  • 257
  • 1
  • 2
  • 8
23
votes
3 answers

Using pivot on multiple columns of an Oracle row

I have the following sample data in an Oracle table (tab1) and I am trying to convert rows to columns. I know how to use Oracle pivot on one column. But is it possible to apply it to multiple columns? Sample data: Type weight height A 50…
Badal
  • 285
  • 1
  • 2
  • 8
23
votes
1 answer

Oracle Pivot query gives columns with quotes around the column names. What?

I'm trying to use PIVOT in Oracle and I'm getting a weird result. It's probably just an option I need to set but what I know about Oracle/SQL I could fit into this comment box. Here's an example of my query: with testdata as ( select 'Fred'…
Richard Schaefer
  • 525
  • 3
  • 13
  • 45
23
votes
7 answers

SQL Server 2005 Pivot on Unknown Number of Columns

I am working with a set of data that looks something like the following. StudentName | AssignmentName | Grade --------------------------------------- StudentA | Assignment 1 | 100 StudentA | Assignment 2 | 80 StudentA | Total …
Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
22
votes
3 answers

How to pivot rows into columns in AWS Athena?

I'm new to AWS Athena and trying to pivot some rows into columns, similar to the top answer in this StackOverflow post. However, when I tried: SELECT column1, column2, column3 FROM data PIVOT ( MIN(column3) FOR column2 IN…
Louis
  • 1,123
  • 5
  • 15
  • 24
22
votes
2 answers

How to spread a column in a Pandas data frame

I have the following pandas data frame: import pandas as pd import numpy as np df = pd.DataFrame({ 'fc': [100,100,112,1.3,14,125], 'sample_id': ['S1','S1','S1','S2','S2','S2'], 'gene_symbol': ['a', 'b',…
neversaint
  • 60,904
  • 137
  • 310
  • 477
22
votes
2 answers

pandas pivot table rename columns

How to rename columns with multiple levels after pandas pivot operation? Here's some code to generate test data: import pandas as pd df = pd.DataFrame({ 'c0': ['A','A','B','C'], 'c01': ['A','A1','B','C'], 'c02': ['b','b','d','c'], …
muon
  • 12,821
  • 11
  • 69
  • 88