PIVOT e UNPIVOT SQL
(Le tabelle ed il codice è preso da un articolo di HTML.it)
Spesso serve avere delle tabelle o viste costruite dinamicamente: le colonne che si vogliono rappresentate sono definite mediante i valori di una specifica tabella.
Con SQL Server 2005 e con Oracle 11g si hanno a disposizione questi due nuovi operatori: PIVOT e UNPIVOT.
Una tabella utile al nostro scopo potrebbe essere:
Qui il codice per crearla:
CREATE TABLE Vendite(
ID INT NOT NULL,
Anno INT NOT NULL,
Venditore VARCHAR(50) NOT NULL,
Ammontare FLOAT NOT NULL
)
ALTER TABLE Vendite ADD (
CONSTRAINT dept_pk PRIMARY KEY (ID));
CREATE SEQUENCE dept_seq START WITH 1;
CREATE OR REPLACE TRIGGER dept_bir
BEFORE INSERT ON Vendite
FOR EACH ROW
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
|
Si ipotizzi di avere questi dati racconti in tabella:
| ID | Anno | Venditore | Ammontare |
|---|---|---|---|
| 1 | 2009 | Marco | 143,3 |
| 2 | 2009 | Andrea | 129 |
| 3 | 2009 | Carlo | 90,5 |
| 4 | 2009 | Roberto | 111 |
| 5 | 2009 | Luigi | 120,1 |
| 6 | 2008 | Marco | 130,7 |
| 7 | 2008 | Andrea | 170,9 |
| 8 | 2008 | Carlo | 167 |
| … | … | … | … |
| … | … | … | … |
Qui il codice per popolare la tabella:
Aggregando per anno mediante:
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2009, 'Marco', 143.3);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2009, 'Andrea', 129);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2009, 'Carlo', 90.5);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2009, 'Roberto', 111);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2009, 'Luigi', 120.1);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2008, 'Marco', 130.7);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2008, 'Andrea', 170.9);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2008, 'Carlo', 167);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2008, 'Roberto', 120.6);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2008, 'Luigi', 131);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2007, 'Marco', 112.5);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2007, 'Andrea', 140.2);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2007, 'Carlo', 132);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2007, 'Luigi', 137.5);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2006, 'Marco', 135.5);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2006, 'Andrea', 99);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2006, 'Carlo', 102.4);
INSERT INTO Vemdite(Anno, Venditore, Ammontare) VALUES (2006, 'Luigi', 116);
|
Aggregando per anno mediante:
SELECT Anno, SUM(Ammontare) as TotaleVendite
FROM Vemdite
GROUP BY Anno
ORDER BY Anno DESC; |
si ottiene:
| Anno | TotaleVendite |
|---|---|
| 2009 | 593,9 |
| 2008 | 720,2 |
| 2007 | 522,2 |
| 2006 | 452,9 |
Ma quello che ci serve ora è ottenere un'analisi "per anno", qualcosa del genere:
| 2006 | 2007 | 2008 | 2009 |
|---|---|---|---|
| 452,9 | 522,2 | 720,2 | 593,9 |
Per agevolare questa operazione, ottenibile anche con comandi SQL molto più complessi si è fornito l'operatore PIVOT:
SELECT * FROM
(SELECT Anno, Ammontare
FROM Vemdite)
PIVOT
(
SUM(Ammontare)
FOR Anno IN (2006, 2007, 2008, 2009)
) |
L'operazione speculare è l'UNPIVOT che ruota le colonne in righe.

Quest'opera è distribuita con Licenza Creative Commons Attribuzione - Condividi allo stesso modo 4.0 Internazionale.


Commenti
Posta un commento