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

Can you use pivot_wider to create multiple groups of alternating new columns?

My data currently looks like this, with the column "Number_Code based on each different Side_Effect: Session_ID Side_Effect Number_Code 1 anxious 1 1 dizzy 2 1 relaxed 3 3 …
alex
  • 858
  • 5
  • 14
14
votes
3 answers

parallel pivot_longer of two sets of columns

I have the following data frame: library(tidyverse) dat <- tribble( ~Scenario, ~V1, ~V2, ~V3, ~V4, 1, 0.97, 0.46, 0.79, 0.25, 1, 0.21, 0.45, 0.23, 0.63, 1, 0.95, 0.97, 0.07, 0.61, 1, 0.93, 0.79,…
deschen
  • 10,012
  • 3
  • 27
  • 50
14
votes
1 answer

Unable to use Common Table Expressions in Postgres Crosstab Query

I'm trying to use perform a pivot operation on some data using Postgres' tablefunc extension's CROSSTAB function. The data needs to undergo some transformations first, which I perform in some common table expressions. However, it appears CROSSTAB…
bosticko
  • 763
  • 9
  • 21
14
votes
2 answers

Dynamically column names from select sql

I have the following data output from my database Observation 1 aug -2015 Improvement suggestion 1 dec -2015 Observation 1 dec -2015 Accident 2 jan -2016 Non Conformity 5 jan…
Pochen
  • 2,871
  • 3
  • 22
  • 27
14
votes
4 answers

Pivot in Excel without aggregation, to show text, not numbers?

Let's say I have a table like this: Country Region Mytext USA North a USA South b Brasil North c Brasil South d How can I obtain a pivot like this in Excel? Country North South USA a b Brasil c …
Pythonista anonymous
  • 8,140
  • 20
  • 70
  • 112
14
votes
4 answers

Using pivot table with column and row totals in sql server 2008

I have a table with following columns defect_id, developer_name, status, summary, root_cause, Secondary_RC, description, Comments, environment_name The column root_cause has Enviro, Requi, Dev, TSc, TD, Unkn as its values and column…
user1931944
  • 175
  • 2
  • 2
  • 9
14
votes
1 answer

Pivoting data in MS Access

I have a query that I've created to pull student IDs and meal items they have taken over a month long period. I would like to count the numbers of each item (Breakfast, Lunch, Snack) taken by a student over the course of the month. It appears…
user2382144
  • 207
  • 2
  • 4
  • 9
14
votes
2 answers

setPivotX works strange on scaled View

I found out that setPivotX (also setPivotY) works strange in Android. If you set pivot when view's scale is set to 1.00f nothing happens (just pivot changes). But if the scale isn't equal 1.0f (e.g. setScaleX(0.9f)) and you set the pivot the view…
tomrozb
  • 25,773
  • 31
  • 101
  • 122
14
votes
3 answers

mysql pivot/crosstab query

Question 1: I have a table with the below structure and data: app_id transaction_id mobile_no node_id customer_attribute entered_value 100 111 9999999999 1 Q1 2 100 111…
DarkKnightFan
  • 1,913
  • 14
  • 42
  • 61
13
votes
4 answers

DataBound Pivot control is not creating the first PivotItem

In a Windows Phone 7 page I have the following control:
dkackman
  • 15,179
  • 13
  • 69
  • 123
13
votes
5 answers

Have Pandas column containing lists, how to pivot unique list elements to columns?

I wrote a web scraper to pull information from a table of products and build a dataframe. The data table has a Description column which contains a comma separated string of attributes describing the product. I want to create a column in the…
C. Hale
  • 145
  • 1
  • 6
13
votes
1 answer

MS SQL Server pivot table with subquery in column clause

Im sure this is a simple technique although I can't find an answer so far! I have TIMESTAMP | POINTNAME | VALUE 2012-10-10 16:00:00 AHU01 20 2012-10-10 16:00:00 AHU02 25 2012-10-10 16:00:15 AHU01 …
user1801843
  • 143
  • 1
  • 1
  • 6
13
votes
2 answers

SQL Server PIVOT - Multiple Aggregates

Given the following result set: --------------------------------------------------------- CustomerID  Service  TransType  SubTotal   Tax   NetTotal --------------------------------------------------------- 106           A        CREDIT     12.52   …
Gus Cavalcanti
  • 10,527
  • 23
  • 71
  • 104
12
votes
2 answers

Many To Many Table Join With Pivot

I currently have two tables similar to users and programs that are linked through a many-to-many relationships by way of a link table. mysql> select * from users; +----+----------+ | id | name | +----+----------+ | 1 | Jonathan | | 2 | Little …
karan.dodia
  • 384
  • 1
  • 4
  • 17
12
votes
2 answers

Pandas pivot produces "ValueError: Index contains duplicate entries, cannot reshape"

I have a pandas table formatted as following: anger_metric metric_name angle_value 0 71.0991 roll 14.6832 1 71.0991 yaw 0.7009 2 71.0991 pitch 22.5075 3 90.1341 roll 4.8566 4 90.1341 yaw 6.4458 5 90.1341 pitch …
A_Matar
  • 2,210
  • 3
  • 31
  • 53