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
16
votes
5 answers

T-SQL dynamic pivot

Ok I have a table that looks like this ItemID | ColumnName | Value 1 | name | Peter 1 | phone | 12345678 1 | email | peter@host.com 2 | name | John 2 | phone | 87654321 2 | email |…
Nicolai Heilbuth
  • 227
  • 1
  • 2
  • 8
15
votes
1 answer

Please explain the parts of a PIVOT

I have read lots of blog posts. I have read the docs. I am usually fairly good at picking up new stuff but even though I keep reading, but I just don't understand the parts of a PIVOT in SQL Server (2008). Can someone please give it to me, nice…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
15
votes
3 answers

ValueError when trying to have multi-index in DataFrame.pivot

I have read pandas: how to run a pivot with a multi-index? but it could not solve my problem. Given the data frame below: import pandas as pd df = pd.DataFrame({ "date": ["20180920"] * 6, "id": ["A123456789"] * 6, "test": ["a", "b", "c",…
ytu
  • 1,822
  • 3
  • 19
  • 42
15
votes
1 answer

Pandas: How to pivot one column in rows into columns

Given this dataframe: feature score searchTerm 0 a 0.534509 pizza 1 b 0.586020 pizza 2 c 0.588972 pizza 3 a 0.566261 chinese 4 b 0.572405 chinese 5 c 0.489369 chinese 6 a 0.499068 thai 7 b …
samol
  • 18,950
  • 32
  • 88
  • 127
15
votes
6 answers

SQL - columns for different categories

I am new to SQL. I have a database with data for different exams, for example: Student Test Grade -------------------- St1 T1 A St2 T1 B St3 T1 B St1 T2 B St2 T2 B St3 T2 A St1 T3 A St2 T3 C St3 T3 …
Michel Mesquita
  • 743
  • 7
  • 17
15
votes
1 answer

Multi-index pivoting in Pandas

Consider the following dataframe: item_id hour when date quantity 110 0YrKNYeEoa 1 before 2015-01-26 247286 111 0UMNiXI7op 1 before 2015-01-26 602001 112 0QBtIMN3AH 1 before 2015-01-26 …
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
15
votes
2 answers

How to pivot dynamically with date as column

I have a table with product id's and names, and another table with the stock of these products on certain dates. Such as Item1 had 6 stock on 1-1-2014 and 8 stock on 2-1-2014. I'm trying to show these in a stored procedure so that it looks like a…
Craphex
  • 153
  • 1
  • 1
  • 5
15
votes
2 answers

Laravel attach with extra field

I have 3 tables, products, images and product_image. The 3rd one is a pivoting table. Other than product_id and image_id in product_image table, I also have 2 extra fields in this pivoting table: position and type, now for an existing product model…
dulan
  • 1,584
  • 6
  • 22
  • 50
15
votes
1 answer

Laravel pivot: Get model from withPivot()

I'm building an application using Laravel 4 but have stumbled across a problem with the pivot tables. I've got a user model, an establishment model, & a studyLevel model. For the moment to find the establishment a user has been at I use the…
xonorageous
  • 2,281
  • 7
  • 26
  • 35
15
votes
1 answer

PostgreSQL says "return and sql tuple descriptions are incompatible"

I have the following data: ID CLASS VALUE 1 NHB 700905.7243 1 HBW 164216.1311 1 HBO 700905.7243 2 NHB 146023.3792 2 HBW 89543.2972 2 HBO 82152.072 3 NHB 1409818.328 3 HBW 220430.7922 3 HBO …
user3357558
  • 153
  • 1
  • 1
  • 5
15
votes
1 answer

PIVOT with varchar datatype

I´m trying to PIVOT some data in a table, but I cannot do it because I do not find the way to do it using varchar columns. I have this table: And what I need is this: I need to use the 'ug_label' row data as columns. As the datatype is VARCHAR, I…
NicoRiff
  • 4,803
  • 3
  • 25
  • 54
15
votes
2 answers

Pivot Table and Concatenate Columns

I have a database in the following format: ID TYPE SUBTYPE COUNT MONTH 1 A Z 1 7/1/2008 1 A Z 3 7/1/2008 2 B C 2 7/2/2008 1 A Z 3 …
Brandon
  • 6,832
  • 11
  • 38
  • 50
14
votes
5 answers

Create a Pivot Table from a DataTable

I am using C# winforms to create an application that needs to turn a datatable into a pivot table. I have the pivot table working fine from a SQL end, but creating it from a datatable seems trickier. I couldn't seem to find anything built into…
ImGreg
  • 2,946
  • 16
  • 43
  • 65
14
votes
2 answers

What is best performance for Retrieving MySQL EAV results as Relational Table

I want to extract results from EAV (entity-attribute-value) tables, or more specifically entity-metadata tables (think like wordpress wp_posts and wp_postmeta) as a "nicely formatted relational table", in order to do some sorting and/or…
drzaus
  • 24,171
  • 16
  • 142
  • 201
14
votes
2 answers

Change column name while using PIVOT SQL Server 2008

SELECT * FROM EmployeeAttributes PIVOT ( MAX(VALUE) FOR AttributeID IN ([DD14C4C2-FC9E-4A2E-9B96-C6A20A169B2E],[BE8149E2-0806-4D59-8482-58223C2F1735],[23B2C459-3D30-41CA-92AE-7F581F2535D4]) ) P Result EmployeeID …
Rauf
  • 12,326
  • 20
  • 77
  • 126