-
Notifications
You must be signed in to change notification settings - Fork 10
/
ej05_PrestamoDePeliculas.sql
164 lines (143 loc) · 3.4 KB
/
ej05_PrestamoDePeliculas.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
-- crear base de datos
create database ej5_PrestamoDePeliculas;
-- usar la base de datos
use ej5_PrestamoDePeliculas;
-- crear tablas
create table rubro
(
codRubro int not null,
nombRubro varchar(30) not null,
primary key(codRubro)
);
create table pelicula
(
codPel int not null,
titulo varchar(30) not null,
duracion time not null,
año int not null,
codRubro int not null,
primary key(codPel),
foreign key(codRubro) references rubro(codRubro)
);
create table ejemplar
(
codEj int not null,
codPel int not null,
estado varchar(10),
ubicacion varchar(30) not null,
primary key(codEj, codPel),
foreign key(codPel) references pelicula(codPel)
);
create table cliente
(
codCli int not null,
nombre varchar(30) not null,
apellido varchar(30) not null,
direccion varchar(30) not null,
tel int not null,
email varchar(40) not null,
primary key(codCli)
);
create table prestamo
(
codPrest int not null,
codEj int not null,
codPel int not null,
codCli int not null,
fechaPrest date not null,
fechaDev date null,
primary key(codPrest),
foreign key(codEj) references ejemplar(codEj),
foreign key(codPel) references pelicula(codPel),
foreign key(codCli) references cliente(codCli)
);
-- consultas
-- 1
select distinct pr.codCli
from prestamo pr
inner join pelicula pel on pr.codPel = pel.codPel
inner join rubro r on pel.codRubro = r.codRubro
where r.nombRubro <> 'Policial';
-- 2
create view peliculaDeMayorDuracion
as
select * from pelicula p
where p.duracion = (
select max(pel.duracion)
from pelicula pel
);
select pmd.codPel from peliculaDeMayorDuracion pmd
where exists(
select distinct pr.codPel from prestamo pr
);
-- 3
select pr.codCli, pr.codPel, count(*) as cantidadPrestamos
from prestamo pr
group by pr.codCli, pr.codPel
having count(*) > 1;
-- 4
select distinct pr.codCli
from prestamo pr
inner join pelicula pel on pr.codPel = pel.codPel
where pel.titulo = 'Rey León'
and exists(
select distinct pr.codCli
from prestamo pr
inner join pelicula pel on pr.codPel = pel.codPel
where pel.titulo = 'Terminator 3'
);
-- 5
create view peliculasVistasPorMes
as
select pr.codPel, month(pr.fechaPrest) as mes, year(pr.fechaPrest) as año, count(*) as cantidad
from prestamo pr
group by pr.codPel, month(pr.fechaPrest), year(pr.fechaPrest);
create view maxCantVistaPorMes
as
select p.mes, p.año, max(p.cantidad) as cantidad
from peliculasVistasPorMes p
group by p.mes, p.año;
select pvm.mes, pvm.año, pvm.cantidad, pvm.codPel
from peliculasVistasPorMes pvm
left join maxCantVistaPorMes mvm
on pvm.mes = mvm.mes
and pvm.año = mvm.año
and pvm.cantidad = mvm.cantidad;
-- 6
select c.codCli from cliente c
where not exists(
select c.codCli, p.codPel from cliente c, pelicula p
where not exists(
select distinct pr.codPel from prestamo pr
)
);
-- 7
select p.codPel from pelicula p
where not exists(
select distinct pr.codPel from prestamo pr
);
-- 8
select distinct pr.codPel
from prestamo pr
where pr.fechaDev is null;
-- 9
create view prestamosPorPelicula
as
select pr.codPel, count(*) as cantidadPrestamos
from prestamo pr
group by pr.codPel;
select pel.titulo from pelicula pel
inner join prestamo pr on pel.codPel = pr.codPel
group by pr.codPel
having count(*) = (
select max(pp.cantidadPrestamos)
from prestamosPorPelicula pp
);
-- 10
select p.codPel from pelicula p
where not exists(
select e.codPel, e.codEj from ejemplar e
where not exists(
select pr.codPel, pr.codEj from prestamo pr
)
);