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
2
votes
1 answer

Transforming a pandas pivot table with multi-level index

I have a pivot table with the following index (output of mypivot.index): MultiIndex(levels=[[u'DATASET1', u'DATASET2', u'DATASET3'], [3, 6], [u'ALGO1', u'ALGO2', u'ALGO3']], labels=[[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], [0, 0, 0, 1, 1,…
giz
  • 53
  • 5
2
votes
1 answer

Convert categories in columns into multiple columns coded as 1 or 0 based on the unique key in Python

I have data like this: user reg ind prod A Asia Tele TV A Asia Bank Phone A Japan Tele Book B US Fin Paper B US Data Shop B Asia …
Kshitij Yadav
  • 1,357
  • 1
  • 15
  • 35
2
votes
1 answer

converting 3D pandas dataframe to 2d

I have a 3D dataframe with 2 levels of index and one column that looks like this: col1 0 0 67.23 0 1 7382 0 2 43 . . 0 8002 54 0 8003 87 1 0 348 1 1 83 1 2 234 . . 1 8002 …
Birish
  • 5,514
  • 5
  • 32
  • 51
2
votes
2 answers

Filling the ID column of a table NOT using a cursor

Tables have been created and used without and ID column, but ID column is now needed. (classic) I heard everything could be done without cursors. I just need every row to contain a different int value so I was looking for some kind of row number…
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
2
votes
1 answer

How to change pivot in OpenGL?

I've developed 3D application. I want to change pivot between model center and user position. When I toggle changing pivot option, 3D model is moved to some position. My partial source below. Please help me. glm::vec2 mouse_delta = mouse_move -…
yhKim
  • 21
  • 3
2
votes
2 answers

Sum values from multiple tables grouping by a common column

I have three tables in MS SQL Server 2014. Each of them holds a couple of numeric values, a description and a date. For the sake of brevety, let's assume the following tables: table "beverages" day beverage amount ---------- --------…
2
votes
3 answers

Pivot Columns to Rows in SQL Server

I have a query that returns an entire row and I need to pivot this result into a new table. SELECT id_no, stud_name, group_no, class_1, class_2, class_3, class_4 FROM tbl_stud_class This returns the following: | id_no | stud_name | group_no |…
Smiley
  • 3,207
  • 13
  • 49
  • 66
2
votes
2 answers

Subtract a subset of columns from a key column in Pandas Pivot

I have a pivot table with multiple columns of data in a time series: A B C D 11/1/2018 1 5 5 7 11/2/2018 2 6 6 8 11/3/2018 3 7 7 9 The values in the data columns are not important for this…
thesimplevoodoo
  • 153
  • 3
  • 11
2
votes
2 answers

MS SQL Server. Transpose columns of data to rows of data

I have a Details table which can be described by the following: Create table #Details ( Id int identity, HeaderId int, ChannelId int, Value float ) We insert some seed data: insert into #Details (HeaderId, ChannelId, Value) values(1, 0,…
AndyS
  • 53
  • 6
2
votes
3 answers

Data type incompatibility in my Dynamic Pivot script

so I've been working on a dynamic pivot script and I've almost got it to work, but I'm having issues with declared variables. Here's my code: DECLARE @start_date DATE DECLARE @end_date DATE SET @start_date =…
2
votes
1 answer

Pandas Transpose using Pivot

I know there are multiple solutions out there but I am not sure if I can't apply to it my case. Little help would make me understand to use it better next time around: my input data: OID V ED D F SF SPID SP M A V 1 V1 E1 D1 …
timewaste
  • 43
  • 6
2
votes
1 answer

Pivoting multiple columns with repetitions in Python

I have data in a frame that looks like: Region Date Drip Coffee Espresso Latte Other Central 1 5 1 2 3 East 1 3 3 1 4 North 1 5 1 3 2 Central…
HelloToEarth
  • 2,027
  • 3
  • 22
  • 48
2
votes
0 answers

odoo: How to change value of total in pivot reports

I want to get the value of the Total in pivot for example | total_HT | total_con | con(%)| ----------------------------------------- Total | 1110 | 1010 | 200| ----------------------------------------- Table | 100 …
m3asmi
  • 1,252
  • 1
  • 16
  • 36
2
votes
1 answer

VBA Change font color based on backgroud color in pivot table

I'm trying to write a VBA code to change a font color of a cell based on a background color of that cell. Generally, to "hide" record names in two columns ("ID" and "Name"). Cells are colored using conditional formatting. I managed to find a way to…
Tart
  • 305
  • 1
  • 6
  • 20
2
votes
1 answer

Python pivot DataFrame without index columns

It is intrducing nulls in resultant dataframe df.pivot( columns='colname',values='value') Initial DF: colname value 0 bathrooms 1.0 1 bathrooms 2.0 2 bathrooms 1.0 3 bathrooms 2.0 4 property_id 82671.0 enter image…
1 2 3
99
100