Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database Pivoting - what is the purpose?

Tags:

database

pivot

Why would someone want to convert columns into rows (ie pivoting)? Which problem does it solve?

I found some links, but did not get satisfactory answers to the questions.

http://codeghar.wordpress.com/2007/11/03/pivot-tables/

http://msdn.microsoft.com/en-us/library/ms177410%28SQL.105%29.aspx

like image 870
Master Avatar asked Jul 24 '12 05:07

Master


People also ask

What is the purpose of pivoting?

Moving rows to columns or columns to rows (or "pivoting") to see different summaries of the source data. Filtering, sorting, grouping, and conditionally formatting the most useful and interesting subset of data enabling you to focus on just the information you want.

What is pivoting in database?

Pivoting data is a technique that rotates data from a state of rows to a state of columns, possibly aggregating multiple source values into the same target row and column intersection.

What is pivoting in SQL and what is the purpose for pivoting?

PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output. And PIVOT runs aggregations where they're required on any remaining column values that are wanted in the final output.

What is the benefit of using a pivot table?

Another important benefit of pivot tables is that it helps to summarize data in a quick and easy manner. The table helps in making a concise summary out of thousands of rows and columns of unorganized data. With the help of these tables you can summarize large amounts of information into a small space.


1 Answers

As regards to your question " Why would someone want to convert columns into rows (ie pivoting)? Which problem does it solve?"

It's not a matter of "how data looks visually" but rather "how it is to be organized and handled". Namely:

In most of the databases, rows represent "Records - (entity, event, etc)" and columns "Fields (attributes of that entity)". For instance the below is a typical DB representation of data;

Person      |  Birth | Height |
-------------------------------
John        |   1980 |  1.82  |
Smith       |   1987 |  2.02  |

That means; each column represents a particular attribute of "persons" and when you select a particular column, you get that particular attribute of ALL the people. This way, a column is a "Dimension" and all values have the same unit (data type), all are dates, all are lengths, etc.

In financial modeling, however, it is just much convenient to represent data the other way around. For instance a typical in "Monthly Cash Flow" table looks like this.

Item         |  Jan |   Feb |
-----------------------------
Sales        | $100 |  $150 |
Tax          | -$50 |  -$15 |

Note that this kind tabulation in a spreadsheet does NOT adhere database formats, column heading are time, but the values in the columns are monetary values, CONFLICT, we can't do calculations with this columns.

If we PIVOT this table, it becomes

Date         | Sales |  Tax  |
------------------------------
Jan          |  $100 |  -$50 |
Feb          |  $150 |  -$15 |  

Now, column dimensions (heading) and the data inside them are consistent. Date column have all dates, and the others have all $ figures. We can take a column and do vector operations with it.

This is one problem that pivoting solves.

like image 87
mutlu Avatar answered Sep 21 '22 05:09

mutlu