🧚 주목! Beekeeper Studio는 빠르고 현대적이며 오픈 소스 데이터베이스 GUI입니다 다운로드
January 17, 2023 작성자: Matthew Rathbone

A pivot table is a useful tool for summarizing and organizing large amounts of data. In PostgreSQL, you can create a pivot table using the crosstab() function. This function is part of the tablefunc extension, which you will need to enable in your database before using it.

Enable the tablefunc extension

To enable the tablefunc extension, connect to your database using a tool like psql and run the following command:

CREATE EXTENSION tablefunc;

Using crosstab()

Once the extension is enabled, you can use the crosstab() function to create a pivot table. The crosstab() function requires at least three arguments: the name of the pivot table, the SQL query that produces the source data for the pivot table, and the column in the source data that will be used for the pivot table’s rows.

For example, suppose you have a table named sales that contains data about sales transactions, including the date, the product that was sold, and the quantity sold. You could create a pivot table that shows the total quantity sold for each product on each date using the following crosstab() query:

SELECT *
FROM crosstab(
    'SELECT date, product, SUM(quantity)
      FROM sales
      GROUP BY date, product
      ORDER BY date, product',
    'SELECT DISTINCT product FROM sales ORDER BY product'
) AS ct (date text, "Product A" int, "Product B" int, "Product C" int);

This query uses the crosstab() function to create a pivot table named ct, with the date as the rows and the product names as the columns. The crosstab() function takes the results of the inner query, which calculates the total quantity sold for each product on each date, and pivots the data to create the pivot table.

Note that the crosstab() function requires you to specify the names of the columns in the pivot table, which must match the names of the products in the source data. This can be a bit tedious, but it allows you to control the structure of the pivot table and ensure that the data is organized in a useful way.

Once you have created the pivot table, you can use it like any other table in PostgreSQL. You can query it, join it with other tables, and even use it as the source data for another pivot table. Pivot tables are a powerful tool for data analysis, and using them in PostgreSQL can help you quickly summarize and organize large amounts of data. Maybe even turn it into a CTE

Beekeeper Studio는 무료 & 오픈 소스 데이터베이스 GUI입니다

제가 사용해 본 최고의 SQL 쿼리 & 편집기 도구입니다. 데이터베이스 관리에 필요한 모든 것을 제공합니다. - ⭐⭐⭐⭐⭐ Mit

Beekeeper Studio는 빠르고 직관적이며 사용하기 쉽습니다. Beekeeper는 많은 데이터베이스를 지원하며 Windows, Mac, Linux에서 훌륭하게 작동합니다.

Beekeeper의 Linux 버전은 100% 완전한 기능을 갖추고 있으며, 기능 타협이 없습니다.

사용자들이 Beekeeper Studio에 대해 말하는 것

★★★★★
"Beekeeper Studio는 제 예전 SQL 워크플로를 완전히 대체했습니다. 빠르고 직관적이며 데이터베이스 작업을 다시 즐겁게 만들어 줍니다."
— Alex K., 데이터베이스 개발자
★★★★★
"많은 데이터베이스 GUI를 사용해 봤지만, Beekeeper는 기능과 단순함 사이의 완벽한 균형을 찾았습니다. 그냥 작동합니다."
— Sarah M., 풀스택 엔지니어

SQL 워크플로를 개선할 준비가 되셨나요?

download 무료 다운로드