Home > Database > Mysql Tutorial > How to Pivot Data on Multiple Columns using SQL's crosstab() Function?

How to Pivot Data on Multiple Columns using SQL's crosstab() Function?

Barbara Streisand
Release: 2025-01-14 09:57:43
Original
686 people have browsed it

How to Pivot Data on Multiple Columns using SQL's crosstab() Function?

Use Tablefunc to implement multi-column data pivot

The crosstab() function in SQL is often used to convert long format data into wide format data. However, it generally assumes that the columns, except the row name and value columns, are the same for every row with the same row name. This limitation can create challenges when working with data sets with variable extra columns.

One way to handle multivariate pivots is to create a temporary table with a row name column and any additional columns required. Then, use the crosstab() function to pivot the data, using the row name column as the primary grouping column and the additional columns as additional grouping columns.

For example, consider the following sample data:

<code class="language-sql">CREATE TEMP TABLE t4 (
  timeof   timestamp
 ,entity    character
 ,status    integer
 ,ct        integer);

INSERT INTO t4 VALUES 
  ('2012-01-01', 'a', 1, 1)
 ,('2012-01-01', 'a', 0, 2)
 ,('2012-01-02', 'b', 1, 3)
 ,('2012-01-02', 'c', 0, 4);</code>
Copy after login

To pivot this data using multiple variables, you can use the following query:

<code class="language-sql">SELECT *
FROM   crosstab(
   'SELECT entity, timeof, status, ct
    FROM   t4
    ORDER  BY 1, 2, 3'
 ,$$VALUES (1), (0)$$)
 AS ct ("Attribute" character, "Section" timestamp, "status_1" int, "status_0" int);</code>
Copy after login

This query groups the data by the entity and timeof columns and then pivots on the status and ct columns as extra columns. The generated table will be in the following format:

<code>Section | Attribute | status_1 | status_0
2012-01-01 00:00:00 | a         | 1         | 2
2012-01-02 00:00:00 | b         | 3         |
2012-01-02 00:00:00 | c         |           | 4</code>
Copy after login

As we can see, the extra columns Attribute are now shown as separate columns, allowing us to easily compare the different measurements taken on each entity at a given time.

In summary, using the crosstab() function with a temporary table can provide a custom and efficient method of multivariate pivoting. By controlling the order of the columns, we can determine which columns are considered row names, extra columns, category columns, and value columns.

The above is the detailed content of How to Pivot Data on Multiple Columns using SQL's crosstab() Function?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template