• Non ci sono risultati.

Soluzione esercitazione 2 1. Interrogazioni

N/A
N/A
Protected

Academic year: 2021

Condividi "Soluzione esercitazione 2 1. Interrogazioni"

Copied!
2
0
0

Testo completo

(1)

Soluzione esercitazione 2 1. Interrogazioni

/*

Query 1

*/

SELECT tipo_tariffa, anno, SUM(incasso) FROM Fatti F, Tempo Te, Tariffa Ta WHERE F.id_tempo=Te.id_tempo AND F.id_tar=Ta.id_tar

GROUP BY CUBE(tipo_tariffa, anno);

oppure

SELECT tipo_tariffa, anno, SUM(incasso), SUM(sum(incasso)) OVER (partition by anno), SUM(sum(incasso)) OVER (partition by tipo_tariffa), SUM(SUM(incasso)) over ()

FROM Fatti F, Tempo Te, Tariffa Ta WHERE F.id_tempo=Te.id_tempo AND F.id_tar=Ta.id_tar

GROUP BY tipo_tariffa, anno;

/*

Query 2

*/

SELECT mese, SUM(num_chiamate) as TotChiamate, SUM(incasso) as Incasso, RANK() over (ORDER BY SUM(incasso) DESC) as RankIncasso

FROM Fatti F, Tempo Te

WHERE F.id_tempo=Te.id_tempo GROUP BY mese;

/*

Query 3

*/

SELECT mese, SUM(num_chiamate) as TotChiamate,

RANK() over (ORDER BY SUM(num_chiamate) DESC) as RankChiamate FROM Fatti F, Tempo Te

WHERE F.id_tempo=Te.id_tempo AND anno=2003

GROUP BY mese;

/*

Query 4

*/

SELECT data, SUM(incasso),

AVG(SUM(incasso)) OVER (ORDER BY data RANGE BETWEEN INTERVAL '2' day preceding and current row) as MediaUltimi3Giorni

FROM Fatti F, Tempo Te

WHERE F.id_tempo=Te.id_tempo AND mese='07-2003' GROUP BY data;

(2)

/*

Query 5

*/

SELECT mese, anno, SUM(incasso) as Incasso,

SUM(SUM(incasso)) over (PARTITION BY anno ORDER BY mese rows unbounded preceding) as IncassoCumulativo

FROM Fatti F, Tempo Te

WHERE F.id_tempo=Te.id_tempo GROUP BY mese, anno;

/*

Query 6

*/

SELECT mese, regione, SUM(num_chiamate) as tot_chiamate, SUM(incasso) as tot_incasso, SUM(SUM(incasso)) OVER (partition by regione ORDER BY mese rows unbounded preceding) as cumulativo_regione

FROM Fatti F, Tempo Te, Tariffa Ta , Luogo L

WHERE F.id_tempo=Te.id_tempo AND F.id_tar=Ta.id_tar AND F.id_luogo_chiamante=L.Id_luogo AND anno=2004 AND tipo_tariffa='Mattino'

GROUP BY mese, regione

/*

Query 7

*/

SELECT L_Chiamante.citta, mese, SUM(incasso)/count(distinct data) as incasso_medio, SUM(num_chiamate) as n_chiamate, SUM(incasso) as IncassoTOT, SUM(SUM(num_chiamate)) over (partition by L_Chiamante.citta) FROM Fatti F, Tempo Te, Luogo L_Chiamante, Luogo L_Chiamato

WHERE F.id_tempo=Te.id_tempo AND F.id_luogo_chiamante=L_Chiamante.Id_luogo AND F.id_luogo_chiamato=L_Chiamato.Id_luogo

AND L_Chiamato.citta='Citta6' Group by L_Chiamante.citta, mese

2. Possibile vista materializzata

• CREATE MATERIALIZED VIEW Esempio_Vista BUILD IMMEDIATE

REFRESH COMPLETE ON DEMAND AS

SELECT mese, anno, SUM(num_chiamate) as TotChiamate, SUM(incasso) as IncassoTot FROM Fatti F, Tempo Te

WHERE F.id_tempo=Te.id_tempo GROUP BY mese, anno

• Riscrittura della query 3 usando la vista

SELECT mese, totchiamate, rank () over (order by totchiamate desc) FROM Esempio_Vista

WHERE anno=2003;

• Riscrittura della query 5 usando la vista

SELECT mese, anno, IncassoTot, Sum(incasso) over

(partition by anno order by mese rows unbounded preceding) as cumulativo FROM esempio_vista;

Riferimenti

Documenti correlati

We compared the reduced density matrices coming from the discretization of the BW modular Hamiltonian and the exact ones for the ground state of the XX chain with a zero magnetic

we prove a few preparatory statements to conclude with our main result as advertised at the beginning of this section and the Abstract.

We consider a special class of binomial sums involving harmonic numbers and we prove three identities by using the elementary method of the partial fraction decomposition..

Solution proposed by Roberto Tauraso, Dipartimento di Matematica, Universit`a di Roma “Tor Vergata”, via della Ricerca Scientifica, 00133 Roma, Italy. (a) Without loss of generality

The sum of two subspaces

Mi propongo di considerare la questione: “Possono pensare le macchine?” Si dovrebbe cominciare con il definire il significato dei termini macchina e pensare ... Se

(1) lista delle macro-unità esistenti nel tratto o nel sottotratto d’indagine (presenza/assenza); (2) estensione areale di ogni macro-unità e/o percentuale relativa

[r]