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

How to do Pivoting in PostgreSQL

I am new to PostgreSQL. Suppose I have a table as under colorname Hexa rgb rgbvalue Violet #8B00FF r 139 Violet #8B00FF g 0 Violet #8B00FF b 255 Indigo #4B0082 r 75 Indigo #4B0082 g 0 Indigo #4B0082 b 130 Blue #0000FF r …
priyanka.sarkar
  • 25,766
  • 43
  • 127
  • 173
18
votes
3 answers

How to transform vertical data into horizontal data with SQL?

I have a table "Item" with a number of related items, like so: ID Rel_ID Name RelRank --- ------ ---- ------- 1 1 foo 1 2 1 bar 2 3 1 zam 3 4 2 foo2 1 I'm trying to get a query so items with the…
Dan
  • 1,677
  • 5
  • 19
  • 34
17
votes
6 answers

Select multiple sums with MySQL query and display them in separate columns

Let's say I have a hypothetical table like so that records when some player in some game scores a point: name points ------------ bob 10 mike 03 mike 04 bob 06 How would I get the sum of each player's scores and display them side by…
SemperFly
  • 1,563
  • 3
  • 17
  • 31
17
votes
2 answers

Pivot multiple columns based on one column in SQL Server

I have the following source and destination tables in SQL Server 2008R2. How can I do pivot(s) in TSQL to transform SourceTbl into DestTbl? Hoping that the empIndex will somehow help in the pivot. SourceTbl empId empIndex empState empStDate…
BJ Rocking
  • 173
  • 1
  • 1
  • 4
17
votes
3 answers

SQL Server - Dynamic PIVOT Table - SQL Injection

Sorry for the long question but this contains all the SQL I've used to test the scenario to hopefully make it clear as to what I'm doing. I'm build up some dynamic SQL to produce a PIVOT table in SQL Server 2005. Below is code to do this. With…
Robin Day
  • 100,552
  • 23
  • 116
  • 167
17
votes
3 answers

Database Pivoting - what is the purpose?

Why would someone want to convert columns into rows (ie pivoting)? Which problem does it solve? I found some links, but did not get satisfactory answers to the…
Master
  • 415
  • 2
  • 4
  • 11
17
votes
2 answers

SQL Pivot with String

I have two tables in SQL Server: Customer and Address Customer Table: CustomerID FirstName LastName ----------- ---------- ---------- 1 Andrew Jackson 2 George Washington Address Table: AddressID CustomerID …
kthiagar
  • 395
  • 1
  • 3
  • 8
16
votes
8 answers

How do I hide a PivotItem?

I have a Page with an Pivot-control and in some cases I don't want to show a particular PivotItem. Setting the Visibility to collapsed doesn't seem to affect it at all. Any suggestions?
Jimmy Engtröm
  • 1,998
  • 4
  • 23
  • 34
16
votes
1 answer

Pandas - group by column and transform the data to numpy array

Having the following data frame, group A have 4 samples, B 3 samples and C 1 sample: group data_1 data_2 0 A 1 4 1 A 2 5 2 A 3 6 3 A 4 7 4 B 1 4 5 B …
Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186
16
votes
2 answers

Turn Presto columns to rows via rotation

This is the desired input and desired output. I'm unfamiliar with the terms used in SQL or Presto and the documentation seems to point to using map_agg but I think the problem here is dynamically creating columns but was curious if this is possible…
dalanmiller
  • 3,467
  • 5
  • 31
  • 38
16
votes
3 answers

Pandas NaN introduced by pivot_table

I have a table containing some countries and their KPI from the world-banks API. this looks like . As you can see no nan values are present. However, I need to pivot this table to bring int into the right shape for analysis. A…
Georg Heiler
  • 16,916
  • 36
  • 162
  • 292
16
votes
2 answers

Pandas pivot table arrangement no aggregation

I want to pivot a pandas dataframe without aggregation, and instead of presenting the pivot index column vertically I want to present it horizontally. I tried with pd.pivot_table but I'm not getting exactly what I wanted. data = {'year': [2011,…
DougKruger
  • 4,424
  • 14
  • 41
  • 62
16
votes
1 answer

How to use groupby in pandas to calculate a percentage / proportion total based on a criteria in another column

I'm trying to work out how to use the groupby function in pandas to work out the proportions of values per year with a given Yes/No criteria. For example, I have a dataframe called names: Name Number Year Sex Criteria 0 name1 789 1998 …
fuzzy_logic_77
  • 387
  • 1
  • 4
  • 14
16
votes
5 answers

SQL Server 2008 Vertical data to Horizontal

I apologize for submitting another question on this topic, but I've read through many of the answers on this and I can't seem to get it to work for me. I have three tables I need to join and pull info on. One of the tables is only 3 columns and…
Fill in the Blank
  • 173
  • 1
  • 1
  • 4
16
votes
2 answers

How to merge two rows into one row in sql?

I have a table as EmployeeID IndividualPay FamilyPay IsActive 1 200 300 true 1 100 150 false But I want the output as follows(I want to use this output to inner join with some…
user1882705
  • 1,081
  • 4
  • 15
  • 43