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

How to Pivot-and-Sort for two columns in python?

I have a super-large dataframe of customers, item categories and their price. I would like to do some initial investigations: Identify the top e.g n=5 customers based on their TOTAL spending. for each of those customer, identify the top categories…
physiker
  • 889
  • 3
  • 16
  • 30
2
votes
2 answers

SQL Server Pivot Data without aggregation

I know this has been asked many, many times before, but I struggle to find the solution to what I need. Data: Id FKId TypeId Score ScoreDate 1 317 1 90 2019-01-01 2 317 1 80 2019-01-02 3 317 2 …
mieliespoor
  • 945
  • 2
  • 8
  • 23
2
votes
0 answers

Dynamically compare week to week by cohorts

Objective: Get Id logins in week 1. Then how many of those Ids logged in in Week 2. Restart the same logic for Week 2 to Week 3. Then week 3 and week 4 and so on... This exercise needs to be done every week. The Ids need to be segmented by…
Roger Steinberg
  • 1,554
  • 2
  • 18
  • 46
2
votes
2 answers

how to have columns which does not have values for certain records in SQL query

I need to generate a output to show different remarks(from table2) for an id(from table 1), but the remark is not mandatory to be present in the table 2. Tried Case function but it is bringing NULL values also as an extra line Table: Employee id …
2
votes
1 answer

Android ScaleAnimation seems to ignore pivot

My custom ScaleAnimation seems to ignore its pivot point. Here is my super-call: super(1.0f, widthFactor, 1.0f, heightFactor, pivotX, pivotY); When I set pivotX to 1.0f and pivotY to 0.0f the top-left corner is fixed although it should be the…
ShadowMare
  • 2,087
  • 2
  • 25
  • 27
2
votes
1 answer

Storing dynamic Pivot result into a temporary table in SQL Server

I have the following schema and sample data. create table MyTable ( Id int, Year int, Channel varchar(10), Payments int ) insert into MyTable values (1,2012,'HV',100), (1,2014,'HV',56), …
Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
2
votes
1 answer

Matlab `unstack`: Safe to assume ordering of new columns?

According to the documentation, Matlab's unstack can take this table: S=12×3 table Storm Town Snowfall _____ ____ ________ 3 'T1' 0 3 'T3' 3 1 'T1' 5 3 'T2' 5 1 …
user36800
  • 2,019
  • 2
  • 19
  • 34
2
votes
2 answers

Need Pivot Table for Columns that Have Comma Separated Values

So, I found a couple other solutions to similar questions I have, but not quite exact. I am interpreting survey results in Excel where the survey tool (Qualtrics) has placed responses from multiple select questions ("select all that apply") in a…
Kevin
  • 23
  • 1
  • 1
  • 3
2
votes
2 answers

Multiply the number of columns by the number of rows

I have a table like this: +------+--------+--------+--------+ | TYPE | PROP_X | PROP_Y | PROP_Z | +------+--------+--------+--------+ | 1 | x1 | y1 | z1 | | 2 | x2 | y2 | z2 | | 3 | x3 | y3 | z3 …
archjkee
  • 41
  • 4
2
votes
3 answers

Pandas Pivot Error "Exception: Data must be 1-dimensional"

I am using Pycharm (version 2018.2.4) with Python 3.6.7 running on it. I currently try to use the pandas pivot function, but even the sample code: import pandas as pd df = pd.DataFrame({'foo':['one', 'one', 'one', 'two', 'two','two'],'bar': ['A',…
stookie
  • 41
  • 1
  • 3
2
votes
3 answers

How to count number of rows returned per month in oracle?

I am creating a feature for my application where I need to generate a report for the whole 2018. I need to count all the tickets for 2018. Each ticket has a category. For example: Change of name. Senior Citizen etc. I need to count the number of…
2
votes
3 answers

How to set and group pandas multi-level columns?

I have a dataframe that has the shape like this: PX_LAST PX_OPEN PX_CLOSE ticker source timestamp 0 1 2 3 A LSE 20180101 1 4 5 6 A LSE 20180102 1 7 8 9 B LSE …
Bob Fang
  • 6,963
  • 10
  • 39
  • 72
2
votes
1 answer

pandas get rolling data based on a past year and create columns in a dataframe

I have a dataframe with 3 columns including A , B , C .i need to create columns using B,C columns as shown in the output section below. dataframe: A B C C_1 pink 1971 C_1 pink 1972 C_1 blue 1972 …
user10901983
2
votes
0 answers

How to use GetPivotdata to extract data from a pivot table built from an Olap Cube?

With a normal excel Pivot tables you can easily run a get pivot data to link to a static table to reference, for instance a product not in the same order as the pivot table. in essence it then becomes dynamic so that when the pivot table is updated…
Sean Power
  • 21
  • 3
2
votes
3 answers

Pandas: Partial pivot based on header substring

I'm familiar with the standard Pandas pivot functionality, but what I'm looking to do is a little different - and I'm not married to using pivot. Setup code: import pandas as pd import io csvA = io.StringIO(u''' month chicken_a chicken_b …
elPastor
  • 8,435
  • 11
  • 53
  • 81
1 2 3
99
100