comedor1.sql --- Facultades sin alumnos select * from facultad

Anuncio
comedor1.sql
--- Facultades sin alumnos
select * from facultad where cFclCdg not in
(select distinct cFclCdg from alumno);
select f.* from facultad f left outer join alumno a on f.cFclCdg = a.cFclCdg
where cAlmCdg is null;
--- Listados de todas las facultades con el numero de alumnos que tiene
select cFclCdg, count(*) from alumno group by cFclCdg;
select f.cFclCdg, count(cAlmCdg) from facultad f left outer join alumno a
on f.cFclCdg = a.cFclCdg
group by f.cFclCdg;
--- alumnos que han bebido la bebida b1 y no han la b2
select * from alumno where
cAlmCdg in (select distinct cAlmCdg from consumicion where cBbdCdg = 'b1')
and
cAlmCdg not in (select distinct cAlmCdg from consumicion where cBbdCdg = 'b2');
--- menus de los que se han servido más de 10 comida
select cMenCdg, count(*) from comida
group by cMenCdg
having count(*) > 10;
--- menu con más comidas servidas ??
--- metodo repitiendo la setencia en el having
select cMenCdg, count(*) from comida
group by cMenCdg
having count(*) >= all (select count(*) from comida
group by cMenCdg);
--- metodo ordenado los registros
select top 1 with ties cMenCdg, count(*) from comida
group by cMenCdg
order by count(*) desc;
--- menú con menos comidas servidas ??
---- ejercicio simetrico al anterior
--- metodo repitiendo la setencia en el having
select cMenCdg, count(*) from comida
group by cMenCdg
having count(*) <= all (select count(*) from comida
group by cMenCdg);
--- metodo ordenado los registros
select top 1 with ties cMenCdg, count(*) from comida
group by cMenCdg
order by count(*) asc;
--- Importe total de las comidas servidas por meses
select year(dCmdFch) año, month(dCmdFch) mes, sum (nMenImp) Importe_mensual_comidas
from comida
group by year(dCmdFch), month(dCmdFch) ;
--- Gastos realizado por cada alumno en consumiciones por meses
select cAlmCdg, year(dCnsFch) año , month(dCnsFch) mes,
sum(nBbdPrc * nCnsUnd) importe_mes
from consumicion
group by cAlmCdg, year(dCnsFch), month(dCnsFch);
--- numero de comidas servidas por facultad
select cFclCdg, count(*) from comida c inner join alumno a on c.cAlmCdg = a.cAlmCdg
group by cFclCdg;
--para que aparezcan las facultades sin alumnos comidas ni nada por el estilo
select f.cFclCdg, count(cMenCdg) from comida c inner join alumno a on c.cAlmCdg =
a.cAlmCdg
right outer join facultad f on f.cFclCdg = a.cFclCdg
group by f.cFclCdg;
--- Gasto total tanto en comidas como en bebidas por alumno ???
-- gasto en comidas de un alumno
select cAlmCdg, sum(nMenImp) from comida
group by cAlmCdg;
-- gasto en bebidas de un alumno
select cAlmCdg, sum(nCnsUnd * nBbdPrc) from consumicion
group by cAlmCdg;
Página 1
comedor1.sql
--- resultado final
select a.*, coalesce(importeComidas,0) ImporteEnComidas, isnull(importeBebidas ,0)
ImporteEnBebidas,
coalesce(importeComidas,0) + isnull(importeBebidas ,0) gastoTotal
from alumno a left outer join
(select cAlmCdg, sum(nMenImp) importeComidas from comida group by cAlmCdg) gc
on a.cAlmCdg = gc.cAlmCdg
left outer join
(select cAlmCdg, sum(nCnsUnd * nBbdPrc) importeBebidas from consumicion group by cAlmCdg)
gb
on a.cAlmCdg = gb.cAlmCdg;
-- esto es erroneo no se pueden relacinar estas tablas
select a.cAlmCdg, sum(nCnsUnd * nBbdPrc), sum(nMenImp), sum(nCnsUnd * nBbdPrc) +
sum(nMenImp)
from alumno a left outer join comida c on a.cAlmCdg = a.cAlmCdg
left outer join consumicion co on a.cAlmCdg = co.cAlmCdg
group by a.cAlmCdg;
--- Importe medio de las comidas
select avg(nMenImp) from comida;
--- importe medio de las bebidas en consumiciones
-- media normal
select avg(nBbdPrc) from consumicion;
--- media ponderada
select sum(nBbdPrc * nCnsUnd) / sum(nCnsUnd) from consumicion;
--- numero de unidades servidas por cada bebida y mes
select cBbdCdg, year(dCnsFch) año, month (dCnsFch) mes, sum(nCnsUnd) Unidades from
consumicion
group by cBbdCdg, year(dCnsFch), month (dCnsFch);
--- bebidas que aún no se han consumido
select * from bebida where cBbdCdg not in (select distinct cBbdCdg from Consumicion);
--- alumno que más se han gastado en bebidas ??
select cAlmCdg from consumicion
group by cAlmCdg
having sum(nCnsUnd * nBbdPrc) >= all (select sum(nCnsUnd * nBbdPrc) from consumicion
group by cAlmCdg);
select top 1 with ties cAlmCdg from consumicion
group by cAlmCdg
order by sum(nCnsUnd * nBbdPrc) desc;
--- datos completos de los alumnos que más se han gastado en bebidas ??
select a.* from alumno a inner join (select top 1 with ties cAlmCdg from consumicion
group by cAlmCdg
order by sum(nCnsUnd * nBbdPrc) desc) r on a.cAlmCdg = r.cAlmCdg;
--- alumnos que han comido y no han consumido bebidas
select * from alumno
where cAlmCdg in (select distinct cAlmCdg from Consumicion)
and cAlmCdg not in (select distinct cAlmCdg from Comida);
Página 2
Descargar