当前位置:网站首页>Different methods of PivotTable in SQL tutorial

Different methods of PivotTable in SQL tutorial

2022-06-23 10:08:00 Knowledge fatness

Data professionals often view transaction data , And need PivotTable for further analysis . for example , Banking experts may need to review each transaction to determine how certain accounts are settled , Or the sales analyst may need to review individual transactions to determine the sales of certain products .

As a basic skill of data operation , PivotTables are often the first step in a broader analysis . Because it is so basic , The main worksheet application provides functions for creating PivotTables , But they rely on complete data sets .

In many big data applications , It is not feasible to analyze millions of rows in a worksheet . Although trying to process this data through your favorite programming language and its libraries might work , But it would be more efficient not to query so much data from the beginning . contrary , Can be in SQL Create PivotTables at the query level of .

data

To demonstrate how to create a PivotTable , A simple dataset showing transaction data will be generated .

CREATE TABLE transactions(
    id INTEGER PRIMARY KEY,
    client_id INTEGER,
    value INTEGER
);

under these circumstances , The transaction table contains three fields : Unique for each transaction ID、 The client of the client associated with the transaction ID And the value of the transaction .

 Insert picture description here
For brevity , Only five rows of random data were generated , But the actual production database can easily host hundreds of thousands or even millions of rows of transaction records .

The query should be able to return each client ID Total transaction value of , Instead of extracting the entire data set .

# Pivot function
PIVOT Function is by far the most direct way to create a PivotTable report . Literally , It is designed as a PivotTable utility , Its grammar is easy to understand .

SELECT column AS column_alias,
    
原网站

版权声明
本文为[Knowledge fatness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230952313913.html