Soluzioni

annuncio pubblicitario
COGNOME e Nome_________________________ LOGIN sul PC_________________________________________
Basi di Dati
Prova di SQl del 24-01-2017
Viene valutato quanto memorizzato su file (denominare il file COGNOME.sql)
Facendo riferimento allo schema del database PROVA, scrivere la query SQL corrispondenti alle seguenti
interrogazioni:
1) Elencare, per tutti i fornitori, il codice, il nome, la città, il numero distinto di prodotti NPS nelle spedizioni nelle quali
il fornitore ha almeno due prodotti diversi. Se la città del fornitore non è nota, stampare al suo posto la stringa “Città
indefinita”.
select fornitori.cod,coalesce(citta,Città indefinita) as citta, nome as nome,
count(distinct prcod) as NPS
from fornitori join sped_dettagli on cod=fcod
where fcod in (select distinct fcod
from sped_dettagli
group by fcod,spcod
having count(distinct prcod) >1)
group by cod,citta,nome
cod
F001
F003
F004
F116
F313
F314
F315
citta
MILANO
MILANO
ROMA
VENEZIA
TORINO
Città indefinita
FIRENZE
nome
ROSSI
BIANCHI
DONATI
GILARDI
MOSCONI
ANDREI
MONTELATICI
NPS
3
4
2
4
2
2
2
2) Produrre una tabella che riporti, per ogni prodotto che compare in una spedizione, il numero di volte in cui la città del
prodotto è diversa dalla città di destinazione NCD e dalla città di partenza NCP(separatamente). Elencare il codice del
prodotto, NCD e NCP (opzionale, elencare tutti gli attributi del prodotto, NCD e NCP)
codice
PR01
PR03
PR04
PR05
PR06
PR07
PR08
PR58
nome
TASTIERA_IBM
SCHEDA_PC
SCHEDA_COMP
XT_IBM
M24
AT_IBM
MAC
NOKIA_SET
città
TORINO
MILANO
ROMA
ROMA
TORINO
PAVIA
TORINO
FIRENZE
NCD
4
2
2
2
1
4
2
1
NCP
3
2
1
2
1
3
2
2
select Prodotti.*, NCD, NCP
from
(select prcod,count(distinct cittadest) as
from spedizioni natural join sped_dettagli
where prodotti.citta<>cittadest
part group by prcod) as UNO
natural join
(select prcod,count(distinct cittapart) as
from spedizioni natural join sped_dettagli
where prodotti.citta<>cittapart
group by prcod) as DUE
join prodotti on prcod=cod
NCD
join prodotti on prcod=cod
NCP
join prodotti on prcod=cod
Scarica