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

LINQ Pivot with dynamic columns

I'm trying to create a Pivot using LINQ with dynamic columns. I have created a Pivot in SQL Server where you do not know which columns are going to get used. But don't know how to transfer that into LINQ. Does anyone have links for me to get…
Funky
  • 12,890
  • 35
  • 106
  • 161
2
votes
0 answers

How to unstack columns with multiple index in python?

I have a Dataframe like this, d = {'state': [ "Alabama", "Alabama","Alabama", "Alabama","Alabama","Alabama","Alabama","Alabama",], 'county_name': ["Autauga","Autauga","Autauga","Baldwin","Baldwin","Baldwin","Baldwin","Barbour"], …
Jiayu Zhang
  • 719
  • 7
  • 22
2
votes
1 answer

Pivot / Groupby dataframe with duplicates in index column with non numeric data

let's assume I have the following example dataframe: df1 = pd.DataFrame({'col1': ['A', 'A', 'B', 'A'], 'col2': ['CA', 'DA', 'CA', 'CA'], 'col3': [1, 1, 1, 2]}) Out[25]: col1 col2 col3 0 A CA 1 1 A DA 1 2 B CA 1 3 A CA …
PV8
  • 5,799
  • 7
  • 43
  • 87
2
votes
0 answers

VBA - Set multiple filters at once in a pivot table without updating in between

I have a script that automates setting some filters on a pivot table depending on the data (hiding and showing certain articles). Now the script works great but when I have a pivot chart it gets super slow. I think what's happening in the background…
superuser
  • 125
  • 7
2
votes
1 answer

Pivot multiple rows / columns into 1 row

We need to take multiple rows and multiple columns and transpose them into 1 row per key. I have a pivot query, but it is not working. I get some error about "Column ambiguously defined' Our data looks like this: SECTOR TICKER …
Landon Statis
  • 683
  • 2
  • 10
  • 25
2
votes
3 answers

Why I get 4 rows in Pivot?

Pivoting Data Pivoting is a technique that groups and aggregates data, transitioning it from a state of rows to a state of columns. In all pivot queries, you need to identify three elements: What do you want to see on rows? This element is known as…
justromagod
  • 933
  • 9
  • 20
2
votes
1 answer

python pandas melt/pivot on multiple columns in one step

df have cola colb colc cold cole colf colg 1 x y 10 15 20 25 2 x y 11 16 27 28 3 x y 12 14 20 30 df want cola colb colc colD colE 1 x y 10 20 1 x y …
babz
  • 469
  • 6
  • 16
2
votes
1 answer

Convert pivot table generated from pivottabler package to dataframe

I'm trying to make a pivot table with pivottabler package. I want to convert the pivot table object to dataframe, so that I can convert it to data table (with DT) and render it in Shiny app, so that it's downloadable. library(pivottabler) pt =…
zesla
  • 11,155
  • 16
  • 82
  • 147
2
votes
2 answers

Pandas: Faster way to do complex column transposition with pivot function

Simply put, I need to convert the following input dataframe to the output below. After a few hours struggling to figure out how, by combining multiple previous stackoverflow questions, I could transform dataframe, but it takes so much time for…
Micro_Andy
  • 93
  • 1
  • 9
2
votes
1 answer

How to calculate pivot value from OHLC data

I've a pandas dataset with open, high, low, close and key column. Now I want to group the dataset by key and calculate pivot with the formula - (high + low + close) / 3. Upto this I'm able to do. But the requirement is to shift the calculated data…
BARUN
  • 139
  • 12
2
votes
0 answers

Eager Load Pivot in nested BelongsToMany with Api Resource

I need your help! I'm having problems returning pivot table information when using ApiResources. If I have a model like this: Post.php public function likes() { return $this->belongsToMany(Like::class) ->withPivot(['points']) // I want…
Zalo
  • 642
  • 12
  • 24
2
votes
1 answer

Pivot table with IN operator and WHERE clause

i'm working on SQL query and using operators PIVOT, IN and clause WHERE. When I finished, noticed that my SQL script sort some column(in that case column ANGLE_1) in a different way, help me out please to find where is the problem. SQL query:…
Nick Pietrushka
  • 125
  • 2
  • 11
2
votes
1 answer

SQL · Dynamic pivot: replace NULL to zeros (0)

I need to pivot data, variable: years (dynamic). But for some years I have no data and I get nulls. I need to replace that nulls into 0. I have tried some examples but I can't get the desired result. Thanks Data: DECLARE @query AS NVARCHAR(MAX), …
user3868641
  • 162
  • 2
  • 13
2
votes
1 answer

How to group pivot dynamic array in javascript or MySQL?

Basically I want to pivot dynamic array, example this array I get from MySQL table: [ { ruleAlias: 'DEMAND', date: '2019-03-22', count: 10 }, { ruleAlias: 'VOUCHER', date: '2019-03-25', count: 20 }, { ruleAlias: 'DEMAND', date: '2019-03-25',…
Deni Setiawan
  • 23
  • 1
  • 6
2
votes
1 answer

How to pivot SQL table without aggregate data

I'm trying to pivot my table: CREATE TABLE articles ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, HCode varchar(2) , Style varchar(4) , Color varchar(3) , Layer smallint , HEX varchar(6) ); with some sample values INSERT INTO articles(HCode,…
Greg Ostry
  • 1,161
  • 5
  • 27
  • 52