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

SQL Rows to Dynamics Columns

I would like to convert some rows that is being extracted in my sql query into columns. Not all rows are duplicated in the result. So would like to know how can I convert rows to columns and if no specific value in the specific column, I would like…
Karu3103
  • 81
  • 8
2
votes
2 answers

Why does the IN Statement within the PIVOT clause has to be static

Everytime I use the pivot clause within SQL, I wonder why the IN Statement has to be static and I need to write something like 'A', 'B'. SELECT * FROM ( SELECT key_1, column_name, value_1 FROM table_1 ) PIVOT ( MAX(column_name) FOR…
Rene Moch
  • 23
  • 3
2
votes
3 answers

Laravel, many-to-many relationship among multiple models

I Have multiple models that have many to many relationship Here are the models News Section Categories Sub Categories Approved News Pending News Each News Section can have multiple Categories. Each Category can have multiple Sub Categories.…
Rao
  • 692
  • 1
  • 7
  • 15
2
votes
4 answers

pandas pivot to transform DataFrame

I'm Just trying to play will pivot to transform the data Frame in a way i desired which pivot does but here its not working. Any expert insight please. DataFrame: >>> df1 id item value 0 2225 prize 1.5 1 2225 unit kg 2 2225 prize …
Karn Kumar
  • 8,518
  • 3
  • 27
  • 53
2
votes
2 answers

Make dataframe wide with months (columns) and periods (rows)

I have a dataframe with multiple columns of months and total (13 columns in total) and two periods (half a year, labeled as 1 and 7). I am trying to spread it wide and have it presented as January1 and January7 (for all 12 months + Total and…
Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63
2
votes
2 answers

Formula to fill a two dimensional grid with computed value from a table

Here is a problem to solve with a Google Sheets formula. I have a big table (sheet “data” with headers on the first row) with those columns: A, product reference B: customer C to F, KPI1 to KPI4 On another sheet, a grid of product references…
2
votes
1 answer

Can't access pivot model's id attribute inside of the static::saved() method in Laravel

I can't access pivot model's id attribute. I have one pivot model PivotModel and two models that are connected through this pivot model ModelA class: public function modelB() { return $this->belongsToMany(ModelB::class, 'model_a_model_b',…
Sašo Kovačič
  • 891
  • 1
  • 9
  • 21
2
votes
2 answers

Creating Dynamic Pivot Table Column Names

I have created a pivot table with hard coded column names. The pivot table simply keeps a rolling sum of sales by qty (current month + 11 months back). It was my first time using the PIVOT function properly and the code works fine. SELECT …
Gray Meiring
  • 297
  • 2
  • 3
  • 16
2
votes
1 answer

Selecting group and value sets when using pivot_longer for reshaping

As noted in Reshaping multiple sets of measurement columns (wide format) into single columns (long format) and Elegant solution for casting (spreading) multiple columns of character vectors, you can use the ".value" component of the names_to…
Joe
  • 119
  • 5
2
votes
3 answers

Pandas - How can I stack columns based on datatype?

If I have a dataframe with only two datatypes like below: d = {'col1': [1, 2], 'col2': ['jack', 'bill'], 'col3': [4, 5], 'col4': ['megan', 'sarah']} df = pd.DataFrame(data=d) print(df) col1 col2 col3 col4 0 1 jack 4 megan 1 2…
Chris Macaluso
  • 1,372
  • 2
  • 14
  • 33
2
votes
3 answers

Oracle: How can I pivot an EAV table with a dynamic cardinality for certain keys?

I have the following Entity–attribute–value (EAV) table in Oracle: | ID | Key | Value | |----|-------------|--------------| | 1 | phone_num_1 | 111-111-1111 | | 1 | phone_num_2 | 222-222-2222 | | 1 | contact_1 | friend | |…
Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
2
votes
3 answers

SQL Pivot on string value instead of numeric

I have an existing table with the following data: InputName ResolvedName Type InsertDate Server_1 Cluster_Alias_1 ABC 9/13/2018 7:47:19 PM Server_2 Cluster_Alias_1 ABC 9/13/2018 7:47:19 PM Server_3 Cluster_Alias_1 ABC 9/13/2018…
Jeremy F.
  • 1,778
  • 11
  • 51
  • 86
2
votes
2 answers

How to convert column (category type) into columns without losing columns?

I have a data frame with many columns. I want to turn the values in the category type column ('Series Name)' into columns without losing the other columns. Below you can see what I did: I have this and I use this…
Yana
  • 785
  • 8
  • 23
2
votes
0 answers

aggregate dynamic column values mysql

I have following task table: id | client_id | user_id | designation_id | total_hours ----------------------------------------------------------------- 1 | 14 | 134 | 1 | 4 2 | 14 | 135 | 2…
Azima
  • 3,835
  • 15
  • 49
  • 95
2
votes
2 answers

NULL fields using PIVOT

I request your collaboration because pivot on a table and separating the records by null, but still leave the fields at 0 with NVL Table product | value ----------+------- Shirts | 1200 Caps | 0 Stocks | 0 Glasses | 100 Shoes |…
Gdaimon
  • 269
  • 1
  • 3
  • 16