---- Facturas a clientes de antequera
select fa.* from Factura fa inner join Cliente cl on fa.cClnNif=cl.cClnNif where cClnPbl='pueblo1';
---- Facturas a clientes que nos han comprado más de 50 euros
select cClnNif, sum(nFctArtUnd*nArtPrc) from Factura f inner join FctArt fa on f.nFctN mr=fa.nFctNmr
group by cClnNif
having sum(nFctArtUnd*nArtPrc) > 50;
select * from Factura where cClnNif in (select cClnNif from Factura f inner join FctArt fa on f.nFctNmr=fa.nFctNmr
group by cClnNif
having sum(nFctArtUnd*nArtPrc) > 50); ---- articulos vendidos al cliente 'nfi1'
select distinct cArtFml, cArtCdg from Factura f inner join FctArt fa on f.nFctNmr=fa.nFctNmr
where cClnNif='nif1';
---- articulos vendidos en las facturas del cliente 'nif1' ---es el mismo que el de arriba
---- facturas que no tienen el articulo 'f1' 'a1' calculo de las facturas que tienen f1 a1
select nFctNmr from FctArt where cArtFml='f1' and cArtCdg='a1'; solucion
select * from factura where nFctNmr
not in (select nFctNmr from FctArt where cArtFml='f1' and cArtCdg='a1'); ---- clientes sin facturas
calculo de los clientes que tienen facturas select distinct cClnNif from factura; select *from cliente where cClnNif
not in (select distinct cClnNif from factura); solucion con left outer join
---- Articulos que no se han vendido
select distinct cArtFml, cArtCdg from FctArt;
select * from Articulo where (cArtFml + cArtCdg) not in (select distinct cArtFml + cArtCdg from FctArt);
---- Limpiadores que no se han estrenado
select cLmpNif from limpiador where cLmpNif not in (select distinct cLmpNif from lavado);
---- Limpiadores que han limpiado vehiculos que ha limpiado en limpiador 'lnif1' select cVhcMtr from lavado where cLmpNif = 'lnif1';
select distinct cLmpNif from lavado where cVhcMtr in
(select distinct cVhcMtr from lavado where cLmpNif = 'lnif1');
---- Vehiculos que se han lavado más veces que el vehiculo con matriculo m1 --vehiculo y numero de lavado de cada vehiculo
select cVhcMtr, count(*) from lavado group by cVhcMtr
having count(*) > (select count(*) from lavado where cVhcMtr = 'm1');
--- prueba de la función case sobre la tabla notas create table notas (
cAlmNmb char(30),
nNotNmr decimal (2,0) check (nNotNmr between 0 and 10) ); insert into notas values ('cebrian', 4);
insert into notas values ('humanes', 5); insert into notas values ('tirado', 6); insert into notas values ('campaña',1); select * from notas;
select cAlmNmb, case nNotNmr when 0 then 'Muy deficiente' when 1 then 'Muy deficiente'
when 2 then 'Muy deficiente' when 3 then 'Insuficiente' when 4 then 'Insuficiente' when 5 then 'Suficiente' when 6 then 'Suficiente' when 7 then 'Bien'
when 8 then 'Notable' when 9 then 'Sobresaliene' when 10 then 'Matricula' else 'Error en los datos' end nota_texto
from notas;
--- ejemplo de case para los mese del año create table fecha (
miFecha datetime);
insert into fecha values ('1-1-2008'); insert into fecha values ('1-4-2008'); insert into fecha values ('1-7-2008'); insert into fecha values ('1-10-2008');
select case month(miFecha) when 1 then 'enero' when 2 then 'febrero' when 3 then 'marzo' when 4 then 'abril' when 5 then 'mayo' when 6 then 'junio' when 7 then 'julio' when 8 then 'agosto' when 9 then 'septiembre' when 10 then 'octubre' when 11 then 'noviembre' when 12 then 'diciembre' end mes
--- formato de la sentencia CASE con condiciones sobre notas: select cAlmNmb, case when nNotNmr < 3 then 'Muy deficiente'
when nNotNmr < 5 then 'Insuficiente' when nNotNmr < 7 then 'Suficiente' when nNotNmr < 8 then 'Bien'
when nNotNmr < 9 then 'Notable'
when nNotNmr < 10 then 'Sobresaliene' when nNotNmr = 10 then 'Matricula' else 'Error en los datos'
end nota_texto from notas;
--- el formato anterio condicional es valido para campos decimales create table notas2 (
cAlmNmb char(30),
nNotNmr decimal (4,2) check (nNotNmr between 0 and 10)); insert into notas2 values ('asfdawere', 3.5);
insert into notas2 values ('asfdads', 2.5); insert into notas2 values ('asfdads', 5.5); insert into notas2 values ('asfdaf', 7.5);
select cAlmNmb, case when nNotNmr < 3 then 'Muy deficiente' when nNotNmr < 5 then 'Insuficiente'
when nNotNmr < 7 then 'Suficiente' when nNotNmr < 8 then 'Bien'
when nNotNmr < 9 then 'Notable'
when nNotNmr < 10 then 'Sobresaliene' when nNotNmr = 10 then 'Matricula' else 'Error en los datos'
end nota_texto from notas2;
insert into cliente values ('nif1a', 'kkkk', 'calle2', 'Vva Trabuco', '44'); insert into cliente values ('nif2a', 'kkkk', 'calle2', 'Vva Rosario', '44'); insert into cliente values ('nif1b', 'kkkk', 'calle2', 'Vva Algaidas', '44'); insert into cliente values ('nif2b', 'kkkk', 'calle2', 'La Roda', '44');
--- clientes de pueblos de nueva implantación select * from cliente where cClnPbl like 'Vva%'; --- cliente cuyo telefono no empieza por 44
select * from cliente where cClnTlf not like '44%'; --- cliente cuyo telefono no empieza por 1
select * from cliente where cClnTlf not like '1%'; --- clientes cuyo telefono empieza por 1
select * from cliente where cClnTlf not like '1%'; ---- cliente cuyo nombre not tiene k
select * from cliente where cClnNmb not like '%k%'; --- clientes cuyo pueblo acaba en 1
select * from cliente where cClnPbl like '%1'; --- clientes cuya tercera letra del nif sea f
select * from cliente where cClnNif like '__f%';
---- Listado de los clientes que tienen más de tres facturas ---- Listado de los clientes que sólo nos han comprado una vez
---- Numero de lavados y numero de vehiculos diferentes que ha lavado cada limpiador ---- Vehiculos cuyos lavados se han factura en facturas del cliente 'nif1'
---- vehiculos cuya matricula es de Malaga formato antiguo
---- Vehiculos cuya matricula es andaluza según formato antiguo ? ---- Vehiculos que se han lavado más de 8 veces.