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
88
votes
3 answers

Construct pandas DataFrame from list of tuples of (row,col,values)

I have a list of tuples like data = [ ('r1', 'c1', avg11, stdev11), ('r1', 'c2', avg12, stdev12), ('r2', 'c1', avg21, stdev21), ('r2', 'c2', avg22, stdev22) ] and I would like to put them into a pandas DataFrame with rows named by the first column…
gt6989b
  • 4,125
  • 8
  • 46
  • 64
66
votes
4 answers

pandas: how to run a pivot with a multi-index?

I would like to run a pivot on a pandas DataFrame, with the index being two columns, not one. For example, one field for the year, one for the month, an 'item' field which shows 'item 1' and 'item 2' and a 'value' field with numerical values. I want…
Pythonista anonymous
  • 8,140
  • 20
  • 70
  • 112
63
votes
7 answers

How to replace (null) values with 0 output in PIVOT

I tried to convert the (null) values with 0 (zeros) output in PIVOT function but have no success. Below is the table and the syntax I've tried: SELECT CLASS, [AZ], [CA], [TX] FROM #TEMP PIVOT (SUM(DATA) FOR STATE IN ([AZ], [CA], [TX])) AS PVT ORDER…
joe
  • 1,463
  • 7
  • 31
  • 45
60
votes
9 answers

Difference between pivot and pivot_table. Why is only pivot_table working?

I have the following dataframe. df.head(30) struct_id resNum score_type_name score_value 0 4294967297 1 omega 0.064840 1 4294967297 1 fa_dun 2.185618 2 4294967297 1 fa_dun_dev …
jwillis0720
  • 4,329
  • 8
  • 41
  • 74
52
votes
6 answers

How to pivot in SQLite or i.e. select in wide format a table stored in long format?

I'd like to get a table which stores students data in long format and the marks they receive for all of their subjects in one query. This is my table structure: Table: markdetails ## studid ## ## subjectid ## ## marks ## A1 3 …
arams
  • 2,201
  • 5
  • 21
  • 18
51
votes
3 answers

MySQL - sum column value(s) based on row from the same table

I'm trying to get 'Cash', 'Check' and 'Credit Card' totals in new columns based on ProductID from the same table. Table - Payments +-----------+------------+---------------+--------+ | ProductID | SaleDate | PaymentMethod | Amount…
sajinshrestha
  • 735
  • 1
  • 6
  • 11
49
votes
4 answers

Python pandas groupby aggregate on multiple columns, then pivot

In Python, I have a pandas DataFrame similar to the following: Item | shop1 | shop2 | shop3 | Category ------------------------------------ Shoes| 45 | 50 | 53 | Clothes TV | 200 | 300 | 250 | Technology Book | 20 | 17 | 21 …
Davide Tamburrino
  • 581
  • 1
  • 5
  • 11
48
votes
9 answers

Transpose column to row with Spark

I'm trying to transpose some columns of my table to row. I'm using Python and Spark 1.5.0. Here is my initial table: +-----+-----+-----+-------+ | A |col_1|col_2|col_...| +-----+-------------------+ | 1 | 0.0| 0.6| ... | | 2 | 0.6| 0.7|…
Raouf
  • 989
  • 2
  • 11
  • 15
48
votes
3 answers

Select user having qualifying data on multiple rows in the wp_usermeta table

I am trying to find the user_id which has all four qualifying values -- each in a different row of the database table. The table that I am querying is wp_usermeta: Field Type Null Key Default …
Mangesh Narayankar
  • 591
  • 2
  • 5
  • 5
44
votes
1 answer

TSQL PIVOT MULTIPLE COLUMNS

I have the following table but unsure of whether it is possible to pivot this and retain all the labels. RATIO RESULT SCORE GRADE Current Ratio 1.294 60 Good Gearing Ratio 0.3384 70 Good Performance Ratio…
Uchenna Ebilah
  • 1,051
  • 2
  • 10
  • 14
42
votes
3 answers

MySQL pivot table query with dynamic columns

I'm using the following tables for storing product data: mysql> SELECT * FROM product; +---------------+---------------+--------+ | id | name | description | stock | +---------------+---------------+--------+ | 1 | product1 | first product |…
fr0sty
  • 553
  • 1
  • 6
  • 10
40
votes
5 answers

SQL Transpose Rows as Columns

I have an interesting conundrum which I believe can be solved in purely SQL. I have tables similar to the following: responses: user_id | question_id | body ---------------------------- 1 | 1 | Yes 2 | 1 | Yes 1 …
Topher Fangio
  • 20,372
  • 15
  • 61
  • 94
40
votes
6 answers

What is the opposite of GROUP_CONCAT in MySQL?

I seem to come against this problem a lot, where I have data that's formatted like this: +----+----------------------+ | id | colors | +----+----------------------+ | 1 | Red,Green,Blue | | 2 | Orangered,Periwinkle…
Jason Hamje
  • 511
  • 1
  • 5
  • 15
38
votes
1 answer

SQL server join tables and pivot

I have two tables with data TABLE 1 --------------------------------------------------- | SALEID | SOLDBY | SALEPRICE | MARGIN | DATE | | 1 | 'aa' | 10,000 | 10 | 2013-1-1 | | 2 | 'bb' | 25,000 | 5 |…
Null Head
  • 2,877
  • 13
  • 61
  • 83
38
votes
2 answers

Mysql query to dynamically convert rows to columns

Can MySQL convert columns into rows, dynamically adding as many columns as are needed for the rows. I think my question might be related to pivot tables but I'm unsure and I don't know how to frame this question other than by giving the following…
Dom
  • 2,980
  • 2
  • 28
  • 41