English version (when available)
select id, idArea from habitacion where dimension = (select max(dimension) from habitacion);
select sip, nombre, apellidos from paciente p, tiene_un t, linea_historial l where p.sip=t.idpaciente and t.idhistorial=l.idhistorial and l.fecha = (select max(fecha) from linea_historial);
select tipo from especialidad e, medico m, trabajador t where e.id=m.idEspecialidad and m.colegiado=t.colegiado and t.fecha_nacimiento = (select min(t1.fecha_nacimiento) from trabajador t1, medico m1 where m1.colegiado=t1.colegiado);
select h.* from habitacion h, area a where h.idArea = a.id and tipo = 'traumatología' and dimension > (select avg(dimension) from habitacion h, area a where h.idArea = a.id and a.tipo = 'traumatología');
select e.tipo, count(*) from medico m, especialidad e where m.idEspecialidad=e.id group by e.id, e.tipo;
select p.sip, p.nombre, p.apellidos, count(*) from paciente p, tiene_un t, linea_historial l where p.sip=t.idpaciente and t.idhistorial=l.idHistorial group by p.sip, p.nombre, p.apellidos order by p.apellidos, p.nombre;
select a.orientacion, count(*) from habitacion h, area a where h.idArea=a.id and not h.ocupado group by a.orientacion;Solución alternativa:ocupado es un atributo booleano por lo que puede tratarse como "NOT ocupado" o "ocupado=0"select a.orientacion, count(*) from habitacion h, area a where h.idArea=a.id and ocupado=0 group by a.orientacion;
select count(*) from linea_historial l where (l.idHistorial, l.id) not in (select idH,idL from ingreso) and (l.idHistorial, l.id) not in (select idH,idL from diagnostico) and (l.idHistorial, l.id) not in (select idH,idL from intervencion) and (l.idHistorial, l.id) not in (select idH,idL from tratamiento) and (l.idHistorial, l.id) not in (select idH,idL from pruebas);
select p.provincia, p.ciudad, count(*) from paciente p group by p.provincia, p.ciudad order by count(*) desc;Solución alternativa:Es necesario agrupar por provincia y por ciudad por dos motivos: a) no se podría mostrar la provincia si no incluye en el group by, b) podría haber dos ciudades con el mismo nombre en diferentes provincias y por tanto deberían formar grupos diferentes.select p.provincia, p.ciudad, count(*) from paciente p group by p.provincia, p.ciudad order by 3 desc;
select a.tipo, count(*) from area a, habitacion h where a.id=h.idArea and not h.ocupado group by a.id, a.tipo order by count(*);
select h.grupo_sanguineo, count(*) from historial h group by h.grupo_sanguineo order by count(*) desc;
select year(p.fecha_nacimiento), count(*) from paciente p group by year(p.fecha_nacimiento) order by year(p.fecha_nacimiento) desc;Solución alternativa:select year(p.fecha_nacimiento), count(*) from paciente p group by year(p.fecha_nacimiento) order by 1 desc;
select year(p.fecha_nacimiento), count(*) from paciente p, historial h, tiene_un t where p.sip=t.idpaciente and t.idhistorial=h.id and h.alergias like '%INSULINA%' group by year(p.fecha_nacimiento) order by year(p.fecha_nacimiento) desc;Solución alternativa:Aunque la solución alternativa proporciona el mismo resultado en nuestra BD, sería más correcta la primera puesto que recogería a los pacientes que tuvieran más de una alergia, por ejemplo "INSULINA, PLATA"select year(p.fecha_nacimiento), count(*) from paciente p, historial h, tiene_un t where p.sip=t.idpaciente and t.idhistorial=h.id and h.alergias = 'INSULINA' group by year(p.fecha_nacimiento) order by year(p.fecha_nacimiento) desc;
select sum(h.dimension), a.tipo from habitacion h, area a where h.idArea=a.id group by a.id, a.tipo order by sum(h.dimension) desc;Si se agrupa únicamente por a.tipo el resultado es el mismo, pero podría darse el caso de que dos áreas tengan el mismo tipo y entonces agruparía los datos de ambas. La solución no sería correcta.
select a.tipo, count(*) from habitacion h, area a where h.idArea=a.id and not h.ocupado group by a.tipo having count(*)<100;
select count(t.nif), a.tipo from area a, trabajador t where a.id=t.idArea group by a.id, a.tipo having count(t.nif)>1900;Solución alternativa:select count(*), a.tipo from area a, trabajador t where a.id=t.idArea group by a.id, a.tipo having count(*)>1900;
select count(t.nif), tu.tipo from asignado a, turno tu where a.idTurno=tu.id and '2014-05-05' between tu.fechainicio and tu.fechafin group by tu.tipo;Solución alternativa:select count(t.nif), tu.tipo from asignado a, turno tu where a.idTurno=tu.id and tu.fechainicio<='2014-05-05' and tu.fechafin>='2014-05-05' group by tu.tipo;
select tu.tipo from asignado a, turno tu where a.idTurno=tu.id and '2014-05-05' between tu.fechainicio and tu.fechafin group by tu.tipo having count(t.nif)<1800;
select nombre, count(*) from paciente group by nombre order by count(*) desc;Solución alternativa:La solución alternativa devuelve exactamente el primero de la lista, es decir, el nombre más frecuente. No obstante, es una construcción que no se han practicado en clase y no se pedirá en esta asignaturaselect nombre from paciente group by nombre having count(*) >= ALL ( select count(*) from paciente group by nombre);
select t.nombre, t.apellidos, e.tipo, count(*) from trabajador t, medico m, especialidad e, linea_historial l where t.colegiado=m.colegiado and m.idEspecialidad=e.id and l.idMedico =t.colegiado group by t.colegiado, t.nombre, t.apellidos, e.tipo;
select t.nombre, t.apellidos, e.tipo from trabajador t, medico m, especialidad e, linea_historial l where t.colegiado=m.colegiado and m.idEspecialidad=e.id and l.idMedico =t.colegiado group by t.colegiado, t.nombre, t.apellidos, e.tipo having count(*)>1;
select distinct t.nombre, t.apellidos, e.tipo from trabajador t, medico m, especialidad e, linea_historial l where t.colegiado=m.colegiado and m.idEspecialidad=e.id and l.idMedico =t.colegiado;Solución alternativa:La solución alternativa devuelve el mismo resultado y puede ir en consonancia con las anteriores propuestas.select t.nombre, t.apellidos, e.tipo from trabajador t, medico m, especialidad e, linea_historial l where t.colegiado=m.colegiado and m.idEspecialidad=e.id and l.idMedico =t.colegiado group by t.colegiado t.nombre, t.apellidos, e.tipo;
No obstante, se puede comprobar que no es necesario agrupar para contar valores >=1 ya que si aparece en el resultado de la concatenación es porque el médico ha firmado al menos una línea de historial.
Igualmente, no tendría sentido añadir un count(*)>=1, aunque devolvería el mismo resultado.
select day(p.fecha_nacimiento), monthname(p.fecha_nacimiento), count(*) from paciente p group by monthname(p.fecha_nacimiento), day(p.fecha_nacimiento) having count(*)>=70 order by month(p.fecha_nacimiento), day(p.fecha_nacimiento);Para la ordenación no sirve la función monthname puesto que daría una ordenación alfabética y no cronológica del mes.