Prac4 SOLUCIONS - 2018 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

361215-Fitxers i bases de dades

Prof. Montserrat Guillén i Catalina Bolancé, Dept. Econometria, UB

Pràctica 4. Més pràctica utilitzant ACCESS - SOLUCIONS

Exercici 1. Heu de crear una base de dades a ACCESS i importar el fitxer EXCEL 361215-prac4-
exercicis-dades.xlsx. Crearem dues taules, una per cadascun dels fulls EXCEL “Respostes” i
“Software”. Definiu les seves dues respectives claus primàries. Deixeu que ACCESS creí
automàticament un Id de la taula Respostes. En la taula Software indiqueu que la clau
primària és ID_Software

Exercici 2.
2.1. Feu una llista dels Software que tenen un nivell baix i guardeu tots els atributs.

SELECT *
FROM Software
WHERE (Nivell=1);

Consulta1
ID_Software Nom_Software TipusSoftware Nivell
1 Windows Sistema Operatiu 1
2 Linux Sistema Operatiu 1
3 Excel Ofimàtica 1

2.2. Seleccioneu tots els casos de la taula Respostes que tinguin una nota de notable (major o
igual a 7 i menor que 9) i llisteu l’ID_resposta, el nom del software i la nota.

SELECT ID_resposta, Nom_Software, Nota INTO Taula22


FROM Respostes
WHERE (Nota>=7 and Nota<9);

SELECT count(ID_resposta)
FROM Taula22
WHERE (Nota>=7 and Nota<9);

375

2.3. Seleccioneu els ID_resposta de la taula Respostes de tots aquells que tenen una nota
superior a 7 amb nivell 2 de software. No dupliqueu ID_resposta.

SELECT DISTINCT ID_Resposta INTO Taula23


FROM Respostes
INNER JOIN Software ON Respostes.Nom_Software=Software.Nom_Software
WHERE (Nota>7) AND (Nivell=2);

SELECT count(ID_Resposta)
FROM Taula23;

13

1
Sense INNER JOIN també és possible:

SELECT DISTINCT ID_Resposta


FROM Respostes, Software
WHERE ((Respostes.Nom_Software=Software.Nom_Software)
AND (Nota>7) AND (Nivell=2));

Consulta3c
ID_Resposta
150
37
371
47
54
554
777
831
862
906
926
930
960

2.4. Feu un llistat de tots els casos de la taula Respostes escrivint ID_resposta, nom del
software i nota, però només els corresponents al tipus de software ‘Programació’.

SELECT ID_resposta, Respostes.Nom_Software, Nota INTO Taula24


FROM Respostes
INNER JOIN Software ON Respostes.Nom_Software=Software.Nom_Software
WHERE (TipusSoftware="Programació");

SELECT count(ID_resposta)
FROM Taula24;

149

SQL alternatiu:

SELECT ID_resposta, Respostes.Nom_Software, Nota


FROM Respostes, Software
WHERE ( (Respostes.Nom_Software=Software.Nom_Software) AND
(TipusSoftware="Programació") );

2.5. Feu un llistat de tots els atributs de Respostes i també l’atribut del Tipus de Software de
cada cas. Ordeneu per ID. Elimineu els casos on la nota estigui en blanc.

2
SELECT ID, ID_Resposta, Respostes.Nom_Software, Nota, SoftAprovat,
Software.TipusSoftware
FROM Respostes
INNER JOIN Software ON Respostes.Nom_Software=Software.Nom_Software
WHERE ISNULL(Nota)=FALSE
ORDER BY ID;

1313

SQL alternatiu:

SELECT ID, ID_Resposta, Respostes.Nom_Software, Nota, SoftAprovat,


Software.TipusSoftware
FROM Respostes, Software
WHERE Respostes.Nom_Software=Software.Nom_Software AND
ISNULL(Nota)=FALSE
ORDER BY ID;

2.6. Afegiu a la tabla creada al punt 2.5 el atribut Nivell associat al software.

SELECT ID, ID_Resposta, Respostes.Nom_Software, Nota, SoftAprovat,


Software.TipusSoftware, Software.Nivell
FROM Respostes
INNER JOIN Software ON Respostes.Nom_Software=Software.Nom_Software
WHERE ISNULL(Nota) =FALSE
ORDER BY ID;

1313

2.7. Torneu a fer la llista de l’apartat anterior però ara el Tipus de Software anomeneu-lo
“Categoria”.

SELECT ID, ID_Resposta, Respostes.Nom_Software, Nota, SoftAprovat,


Software.TipusSoftware AS Categoria, Software.Nivell
FROM Respostes
INNER JOIN Software ON Respostes.Nom_Software=Software.Nom_Software
WHERE ISNULL(Nota)=FALSE
ORDER BY ID;

1313

2.8. Feu un llistat dels ID_resposta de la taula Respostes de tots aquells que tenen una nota
inferior a 5 en SAS i tenen el software suspès. No hi ha d’haver repeticions de ID_resposta.

SELECT DISTINCT ID_Resposta


FROM Respostes
WHERE (Nom_Software='SAS') AND (Nota<5) AND (SoftAprovat='n');

3
Consulta8
ID_Resposta
578
87

2.9. Torneu a fer el llistat anterior però ordeneu les Respostes en ordre decreixent de nota de
SAS.

SELECT ID_Resposta
FROM Respostes
WHERE (Nom_Software='SAS') AND (Nota<5) AND (SoftAprovat='n')
ORDER BY Nota DESC;

Consulta9
ID_Resposta
87
578

2.10. Feu un llistat dels ID_resposta i Nota de la taula Respostes de tots aquells que tenen una
nota de R entre 7 i 9 (ambdos inclosos), amb ordre creixent de nota.

SELECT ID_Resposta, Nota INTO Taula210


FROM Respostes
WHERE (Nom_Software='R') AND (Nota >= 7) AND (Nota <= 9)
ORDER BY Nota;

SELECT count(ID_Resposta)
FROM Taula210;

93

2.11. Feu una llista de les Respostes que tenen un valor en blanc (null) a la nota de
“LaTeX.Beamer”.

SELECT * INTO Taula211


FROM Respostes
WHERE (Nom_Software='LaTeX.Beamer') AND (Nota IS null);

SELECT count(ID_resposta)
FROM Taula211;

61

2.12. Calculeu la nota mitjana de la classe de tots els softwares junts

SELECT AVG(Nota)
FROM Respostes;

4,3997

4
2.13. Calculeu la nota mitjana de tots els softwares, sempre i quan la nota sigui superior a 5.

SELECT AVG(Nota)
FROM Respostes
WHERE (Nota>5);

7,43

2.14. Trobeu la nota mitjana de SAS i R a les Respostes. Ho podeu fer en dues consultes
separades.

SELECT AVG(Nota)
FROM Respostes
WHERE (Nom_Software='SAS');

5,58

SELECT AVG(Nota)
FROM Respostes
WHERE (Nom_Software='R');

6,8767

2.15. Calculeu la nota mitjana del tipus de software ‘Sistema Operatiu’

SELECT AVG(Nota)
FROM Respostes
INNER JOIN Software ON Respostes.Nom_Software=Software.Nom_Software
WHERE (TipusSoftware='Sistema Operatiu');

5,751

SQL Alternatiu:

SELECT AVG(Nota)
FROM Respostes , Software
WHERE( (Respostes.Nom_Software=Software.Nom_Software)
AND (TipusSoftware='Sistema Operatiu'));

2.16. Calculeu la nota mitjana dels softwares de bases de dades (etiqueta ‘BigData’).

SELECT AVG(Nota)
FROM Respostes
INNER JOIN Software ON Respostes.Nom_Software=Software.Nom_Software
WHERE (TipusSoftware='BigData');

0,76

5
2.17. Feu un llistat dels ID_resposta de la taula Respostes de tots aquells que tenen una nota
superior en SAS que en R. No dupliqueu ID_resposta.

SELECT DISTICT R1.Id_resposta AS ID_resposta, NOTASAS, NOTAR


FROM
(
SELECT Id_resposta, Nota AS NOTASAS
FROM Respostes
WHERE ( (Nom_software='SAS'))
) AS R1 INNER JOIN
(
SELECT Id_resposta, Nota AS NOTAR
FROM Respostes
WHERE ( (Nom_software='R'))
) AS R2 ON (R1.Id_Resposta=R2.Id_Resposta)
WHERE (NOTASAS>NOTAR);

Consulta17
ID_resposta NOTASAS NOTAR
123 5 3
150 8 7
285 8 7
363 5 4
382 6 4
491 6 5
535 7 6
54 10 9
906 8 7

2.18. Feu un llistat dels ID_resposta de la taula Respostes de tots aquells que tenen una nota
superior a 5 tant en SAS com en R i amb nota superior en R que en SAS. No dupliqueu
ID_resposta.

SELECT DISTINCT R1.Id_Resposta AS ID_resposta,


NOTASAS, NOTAR INTO Taula218
FROM
(SELECT Id_resposta, Nota AS NOTASAS
FROM respostes
WHERE ( (Nom_software='SAS') AND (Nota>5))
) AS R1 INNER JOIN
(SELECT Id_resposta, Nota AS NOTAR
FROM respostes
WHERE ( (Nom_software='R') AND (Nota>5))
) AS R2 ON (R1.Id_Resposta=R2.Id_Resposta)
WHERE NOTAR>NOTASAS;

SELECT count(Id_Resposta) FROM Taula218;

55

You might also like