View
217
Download
6
Category
Preview:
Citation preview
UNIVERSIDAD NACIONAL DE SAN ANTONIO ABAD DEL CUSCO
FACULTAD DE CIENCIAS QUIMICAS, FISICAS Y
MATEMATICAS
INGENIERIA INFORMATICA Y DE
SISTEMAS
“EJERCICIOS PROPUESTOS SENTENCIA SELECT”
Curso : LABORATORIO BASE DE DATOS I
Docente : HERNAN NINA HANCCO
Alumnos : MONTESINOS YNQUILTUPA WILBERT
Cusco-Perú
1.-relacion de prestamos cancelados de un determinado
prestatario
--relacion de prestamos cancelados
select P.DocPrestamo, B.CodPrestatario, B.Nombres, P.Importe,
(P.Importe - SUM(ISNULL(A.Importe, 0))) as Saldo
from (Prestamo P left outer join Amortizacion A on
(P.DocPrestamo = A.DocPrestamo))
inner join Prestatario B on (P.CodPrestatario =
B.CodPrestatario)
group by P.DocPrestamo, B.CodPrestatario, B.Nombres, P.Importe
having (P.Importe - SUM(ISNULL(A.Importe, 0)) = 0)
order by B.CodPrestatario
2.-relacion de prestamos efectuados por los prestatarios de una
determinadacomunidad
--prestamos efectuados por los prestatarios de una determinada
comunidad
select P.DocPrestamo, C.CodComunidad, C.Nombre
from Prestamo P, Prestatario B, Comunidad C
where (P.CodPrestatario = B.CodPrestatario) and (B.CodComunidad
= C.CodComunidad)
and (C.CodComunidad = 'C001')
--order by C.CodComunidad
select * from Comunidad
3.-Relacion de prestatarios q hasta la fecha hayan efectuado mas
de 5 prestamos
select A.CodPrestatario, A.Nombres, COUNT(P.DocPrestamo) as
NroPrestamos
from Prestatario A inner join Prestamo P
on (A.CodPrestatario = P.CodPrestatario)
group by A.CodPrestatario, A.Nombres
having (COUNT(DocPrestamo) > = 5)
4.-Relacion de prestatarios moroso, es decir , aquellos que aun
no han cancelado alguna de sus deudas y ya paso la fecha de
vencimiento
--Tabla de pretamos vencidos q no se an cancelado
select P.DocPrestamo, P.Importe,SUM(A.Importe)as
MontoCancelado,P.CodPrestatario
into ##PrestamoAmort
from Prestamo P,Amortizacion A
where P.DocPrestamo=A.DocPrestamo and
P.FechaVencimiento<SYSDATETIME()
group by P.DocPrestamo,P.Importe,P.CodPrestatario
having SUM(A.Importe)!=P.Importe
--relacion de prestatarios morosos
select P.CodPrestatario,P.Nombres
from ##PrestamoAmort PA,Prestatario P
where PA.CodPrestatario=P.CodPrestatario
group by P.CodPrestatario,P.Nombres
drop table ##PrestamoAmort
go
5.-Relacion de las 5 comunidades que tienen el mayor numero de
prestatarios
--ordenamos las comunidades por numero de prestatarios
select CodComunidad,count(CodPrestatario) as NroPrestatarios
into ##Prestatario
from Prestatario
group by CodComunidad
order by Nroprestatarios
--seleccionamos a las 5 comunidades con mayor numero de prestatarios
select top(5) NroPrestatarios,C.CodComunidad,C.Nombre
from ##Prestatario P,Comunidad C
where P.CodComunidad=C.CodComunidad
order by NroPrestatarios desc
drop table ##Prestatario
go
6.-Relacion de comunidades cuyos prestatarios que aun tienen
saldo, no hayan efectuado ninguna amortizacion en lo que va del
año 2004
--Tabla de prestatarios que aun tienen saldo
select P.DocPrestamo,P.CodPrestatario,P.Importe,SUM(A.Importe)as
MontoCancelado
into ##PrestamoAmort
from Prestamo P,Amortizacion A
where P.DocPrestamo=A.DocPrestamo and
P.FechaVencimiento<SYSDATETIME()
group by P.DocPrestamo,P.CodPrestatario,P.Importe
having SUM(A.Importe)!=P.Importe
select C.CodComunidad,C.Nombre
from
--prestatarios q aun tiene saldo
(select CodPrestatario
from ##PrestamoAmort
group by CodPrestatario
except
--prestatarios q efectuaron amortizacion en el 2004
select CodPrestatario
from ##PrestamoAmort PA inner join Amortizacion A
on PA.DocPrestamo=A.DocPrestamo
where year(A.FechaCancelacion)='2004'
group by CodPrestatario) P, Prestatario Pr,Comunidad C
where P.CodPrestatario=Pr.CodPrestatario and
Pr.CodComunidad=C.CodComunidad
group by C.CodComunidad,C.Nombre
drop table ##PrestamoAmort
go
7.-Relacion de comunidades que no tengan prestatarios morosos
--Tabla de prestatarios morosos
select P.CodPrestatario,P.Importe,SUM(A.Importe)as MontoCancelado
into ##PrestamoAmort
from Prestamo P,Amortizacion A
where P.DocPrestamo=A.DocPrestamo and
P.FechaVencimiento<SYSDATETIME()
group by P.CodPrestatario,P.Importe
having SUM(A.Importe)!=P.Importe
--comunidades q no tengan prestatarios morosos
--total de comunidades
select * from Comunidad
EXCEPT
--Comunidades q tengan morosos
select P.CodComunidad,C.Nombre
from ##PrestamoAmort PA inner join Prestatario P
on P.CodPrestatario=PA.CodPrestatario,Comunidad C
where C.CodComunidad=P.CodComunidad
group by P.CodComunidad,C.Nombre
drop table ##PrestamoAmort
go
8.-Relacion de comunidades que tengan prestatarios morosos
--Tabla de prestatarios morosos
select P.CodPrestatario,P.Importe,SUM(A.Importe)as MontoCancelado
into ##PrestamoAmort
from Prestamo P,Amortizacion A
where P.DocPrestamo=A.DocPrestamo and
P.FechaVencimiento<SYSDATETIME()
group by P.CodPrestatario,P.Importe
having SUM(A.Importe)!=P.Importe
--comunidades q tengan prestatarios morosos
select P.CodComunidad,C.Nombre
from ##PrestamoAmort PA inner join Prestatario P
on P.CodPrestatario=PA.CodPrestatario,Comunidad C
where C.CodComunidad=P.CodComunidad
group by P.CodComunidad,C.Nombre
drop table ##PrestamoAmort
go
9.-Relacion de comunidades con 3 de sus prestatarios mas
importantes(los prestatarios mas importantes
son aquellos que an obtenido mayor numero de prestamos)
--relacion de prestatarios con su respectivo numero de prestamos y
comunidad a la q pertenecen
select top 3 C.CodComunidad, C.Nombre, P.CodPrestatario, B.Nombres,
COUNT(P.CodPrestatario) as NroPrestamos
from Prestamo P, Prestatario B, Comunidad C
where(P.CodPrestatario = B.CodPrestatario) and
(B.CodComunidad = C.CodComunidad)
group by C.CodComunidad, C.Nombre, P.CodPrestatario, B.Nombres
order by NroPrestamos desc
10.-Relacion de prestatarios que en ninguno de sus prestamos
hayan incurrido en mora
--Tabla de prestatarios morosos
select P.CodPrestatario,P.Importe,SUM(A.Importe)as MontoCancelado
into ##PrestamoAmort
from Prestamo P,Amortizacion A
where P.DocPrestamo=A.DocPrestamo and
P.FechaVencimiento<SYSDATETIME()
group by P.CodPrestatario,P.Importe
having SUM(A.Importe)!=P.Importe
--prestatarios q en ninguno de sus prestamos han incurrido en mora
--todos los prestatarios menos lo prestatarios morosos
select P.CodPrestatario,P.Nombres
from (select CodPrestatario from Prestatario
except
select CodPrestatario from ##PrestamoAmort)T inner join
Prestatario P
on T.CodPrestatario=P.CodPrestatario
group by P.CodPrestatario,P.Nombres
drop table ##PrestamoAmort
go
11.-Relacion de prestatarios que en todas las veces que solicito
un prestamo, solo una vez incurrio en mora
--Tabla de prestamos que no fueron cancelados
select P.DocPrestamo,P.CodPrestatario,P.Importe,SUM(A.Importe)as
MontoCancelado
into ##PrestamoAmort
from Prestamo P,Amortizacion A
where P.DocPrestamo=A.DocPrestamo and
P.FechaVencimiento<SYSDATETIME()
group by P.DocPrestamo,P.CodPrestatario,P.Importe
having SUM(A.Importe)!=P.Importe
--relacion de prestatarios q solo una vez incurrio en mora
select CodPrestatario,COUNT(DocPrestamo)as NroMoras
from ##PrestamoAmort
group by CodPrestatario
having COUNT(DocPrestamo)=1
drop table ##PrestamoAmort
go
12.-Relacion de prestatarios que hayan cancelado sus prestamos
sin pagos parciales
--prestamos cancelados
select P.DocPrestamo,P.CodPrestatario,P.Importe,SUM(A.Importe)as
MontoCancelado
into ##PrestamoAmort
from Prestamo P,Amortizacion A
where P.DocPrestamo=A.DocPrestamo and
P.FechaVencimiento<SYSDATETIME()
group by P.DocPrestamo,P.CodPrestatario,P.Importe
having SUM(A.Importe)=P.Importe
--Prestatario con su numero de prestamos cancelados
select CodPrestatario,COUNT(DocPrestamo)as NroPrestaCancel
into ##PrestamCanc
from ##PrestamoAmort
group by CodPrestatario
--Prestamos que fueron cancelados de una sola amortizacion
select CodPrestatario,COUNT (P.DocPrestamo)as NroPrestCancela1
into ##PrestamosCanc1
from Amortizacion A inner join Prestamo P
on A.DocPrestamo=P.DocPrestamo
where A.Importe=P.Importe
group by CodPrestatario
--relacion de prestatarios q cancelaron sus prestamos de una sola
amortizacion
select P.CodPrestatario,P.Nombres
from(select PC.CodPrestatario
from ##PrestamosCanc1 PC1, ##PrestamCanc PC
where PC1.CodPrestatario=PC.CodPrestatario and
PC1.NroPrestCancela1=PC.NroPrestaCancel)PC inner join Prestatario P
on PC.CodPrestatario=P.CodPrestatario
drop table ##PrestamosCanc1
drop table ##PrestamCanc
go
13.-Relacion de los oficiales de credito estrella de cada mes
del año 2003.(Se considera oficial de credito "estrella" del
mes, al oficial de credito que haya colocado el mayor numero de
prestamos en el mes)
--seleccionamos los prestamos del 2003
select MONTH(FechaPrestamo)as Mes,CodOficial,sum(Importe)as ImporteMes
into ##PrestamosMes
from Prestamo
where year(FechaPrestamo)='2007'
group by Month(FechaPrestamo), CodOficial
order by Month(FechaPrestamo)
--oficales estrella por mes
select P.CodOficial,P.Mes
into ##OficialEstr
from ##PrestamosMes P,(select Mes,MAX(ImporteMes)as Monto
from ##PrestamosMes
group by Mes)PM
where PM.Monto=P.ImporteMes
group by P.CodOficial,P.Mes
--oficial estrella y su respectivo mes
select O.CodOficial,O.Nombres,OE.Mes
from ##OficialEstr OE, Oficial_Credito O
where OE.CodOficial=O.CodOficial
drop table ##PrestamosMes
drop table ##OficialEstr
go
/*14.-Relacion de oficiales de credito que no hayan colocado por lo
menos un prestamo en algun mes del año 2003*/
--seleccionamos los oficiales q realizaron prestamos en el 2003
select CodOficial
into ##Prestamos2003
from Prestamo
where year(FechaPrestamo)='2003'
--todos lo oficiales menos los oficiales q no efectuaron ningun
prestamo por lomenos en algun m,es del 2003
select CodOficial
from Oficial_Credito
except
select *
from ##Prestamos2003
drop table ##Prestamos2003
go
15.-Relacion de prestamos en riesgo. Se considera prestamo en
riesgo cuando tiene saldo y a trascurrido mas de 6 meses de su
fecha de vencimiento
--Tabla de prestamos que vencieron y no fueron cancelados
select P.DocPrestamo,P.CodPrestatario,P.CodOficial,P.FechaVencimiento,
P.Importe,SUM(A.Importe)as MontoCancelado
into ##PrestamoVencido
from Prestamo P,Amortizacion A
where P.DocPrestamo=A.DocPrestamo and
P.FechaVencimiento<SYSDATETIME()
group by
P.DocPrestamo,P.CodPrestatario,P.CodOficial,P.FechaVencimiento,P.Impor
te
having SUM(A.Importe)!=P.Importe
--prestamos en riesgo
select DocPrestamo,CodPrestatario,FechaVencimiento
from ##PrestamoVencido
where datediff(dd,FechaVencimiento,getdate())>180
drop table ##PrestamoVencido
go
16.-Relacion de comunidades con los saldos totales de los
prestamos que estan en riesgo
--Tabla de prestamos que vencieron y no fueron cancelados
select
P.DocPrestamo,P.CodPrestatario,P.CodOficial,P.FechaVencimiento,
P.Importe,SUM(A.Importe)as MontoCancelado
into ##PrestamoVencido
from Prestamo P,Amortizacion A
where P.DocPrestamo=A.DocPrestamo and
P.FechaVencimiento<SYSDATETIME()
group by
P.DocPrestamo,P.CodPrestatario,P.CodOficial,P.FechaVencimiento,P.Impor
te
having SUM(A.Importe)!=P.Importe
--prestamos en riesgo
select
DocPrestamo,CodPrestatario,CodOficial,FechaVencimiento
into ##PrestamoRiesgo
from ##PrestamoVencido
where datediff(dd,FechaVencimiento,getdate())>180
--deudas totales por prestatario
select PV.CodPrestatario,(sum(PV.Importe)-
sum(PV.MontoCancelado))as Deuda
into ##SaldoPrestatario
from ##PrestamoVencido PV, ##PrestamoRiesgo PR
where PR.DocPrestamo=PV.DocPrestamo
group by PV.CodPrestatario
--Deudas totales por comuniudad
select C.CodComunidad,C.Nombre,sum(Deuda)as DeudaRiesgo
from ##SaldoPrestatario SP,Prestatario P,Comunidad C
where SP.CodPrestatario=P.CodPrestatario and
C.CodComunidad=P.CodComunidad
group by C.CodComunidad,C.Nombre
drop table ##PrestamoVencido
drop table ##PrestamoRiesgo
drop table ##SaldoPrestatario
go
17.-Relacion de oficiales de credito con los saldos totales de
los prestamos efectuados por ellos que estan en riesgo
----Tabla de prestamos que vencieron y no fueron cancelados
select P.DocPrestamo,P.CodPrestatario,P.CodOficial,P.FechaVencimiento,
P.Importe,SUM(A.Importe)as MontoCancelado
into ##PrestamoVencido
from Prestamo P,Amortizacion A
where P.DocPrestamo=A.DocPrestamo and
P.FechaVencimiento<SYSDATETIME()
group by
P.DocPrestamo,P.CodPrestatario,P.CodOficial,P.FechaVencimiento,P.Impor
te
having SUM(A.Importe)!=P.Importe
--prestamos en riesgo
select DocPrestamo,CodPrestatario,CodOficial,FechaVencimiento
into ##PrestamoRiesgo
from ##PrestamoVencido
where datediff(dd,FechaVencimiento,getdate())>180
--deudas totales por oficial_cresditos
select PV.CodOficial,(sum(PV.Importe)-sum(PV.MontoCancelado))as Deuda
from ##PrestamoVencido PV, ##PrestamoRiesgo PR
where PR.DocPrestamo=PV.DocPrestamo
group by PV.CodOficial
drop table ##PrestamoVencido
drop table ##PrestamoRiesgo
go
18.-Relacion de oficiales de credito que hayan efectuado
prestamos en todas las comunidades
--relacion de oficial de credito
select P.CodOficial,Pr.CodComunidad
into ##OficialComu
from Prestamo P,Prestatario Pr
where P.CodPrestatario=Pr.CodPrestatario
group by P.CodOficial,Pr.CodComunidad
--relacion de oficiales de creito que hayan realizado prestamos en
todas las comunidades
select O.CodOficial,O.Nombres,COUNT(Oc.CodComunidad)as NroComunidades
from ##OficialComu OC,Oficial_Credito O
where OC.CodOficial=O.CodOficial
group by O.CodOficial,O.Nombres
having COUNT(Oc.CodComunidad)=(select COUNT(CodComunidad) from
Comunidad)
drop table ##OficialComu
go
19.-Relacion de comunidades cuyos montos totales de prestamo
hayan disminuido en los dos ultimos años, es decir el monto
total del 2008 sea menor al del 2007 y el monto total del 2007
sea menor al del 2006
drop table #MontoTotalCredito2003
drop table #MontoTotalCredito2002
drop table #MontoTotalCredito2001
select B.CodComunidad, sum(A.Importe) as MontoTotalCredito
into #MontoTotalCredito2003
from Prestamo A inner join Prestatario B on A.CodPrestatario =
B.CodPrestatario
where FechaPrestamo between '01/01/2003' and '31/12/2003'
group by B.CodComunidad
select B.CodComunidad, sum(A.Importe) as MontoTotalCredito
into #MontoTotalCredito2002
from Prestamo A inner join Prestatario B on A.CodPrestatario =
B.CodPrestatario
where FechaPrestamo between '01/01/2002' and '31/12/2002'
group by B.CodComunidad
select B.CodComunidad, sum(A.Importe) as MontoTotalCredito
into #MontoTotalCredito2001
from Prestamo A inner join Prestatario B on A.CodPrestatario =
B.CodPrestatario
where FechaPrestamo between '01/01/2001' and '31/12/2001'
group by B.CodComunidad
select X.CodComunidad, isnull(X.MontoTotalCredito, 0) as monto2003,
isnull(Y.MontoTotalCredito, 0) monto2002, isnull(Z.MontoTotalCredito,
0) as monto2001
from #MontoTotalCredito2003 X left outer join
(#MontoTotalCredito2002 Y left outer join #MontoTotalCredito2001 Z on
Y.CodComunidad = Z.CodComunidad)
on X.CodComunidad = Y.CodComunidad
where isnull(X.MontoTotalCredito, 0) <
isnull(Y.MontoTotalCredito, 0) and isnull(Y.MontoTotalCredito, 0) <
isnull(Z.MontoTotalCredito, 0)
20.-Calcular el indice de morosidad por comunidad. El indice de
morosidad es igual al porcentaje del saldo del prestamo que esta
en riesgo sobre el total del prestamo
--Tabla de prestamos que vencieron y no fueron cancelados
select P.DocPrestamo,P.CodPrestatario,P.CodOficial,P.FechaVencimiento,
P.Importe,SUM(A.Importe)as MontoCancelado
into ##PrestamoVencido
from Prestamo P,Amortizacion A
where P.DocPrestamo=A.DocPrestamo and
P.FechaVencimiento<SYSDATETIME()
group by
P.DocPrestamo,P.CodPrestatario,P.CodOficial,P.FechaVencimiento,P.Impor
te
having SUM(A.Importe)!=P.Importe
--prestamos en riesgo
select DocPrestamo,CodPrestatario,CodOficial,FechaVencimiento,Importe
into ##PrestamoRiesgo
from ##PrestamoVencido
where datediff(dd,FechaVencimiento,getdate())>180
--deudas e improtes totales por prestatario
select PV.CodPrestatario,(sum(PV.Importe)-sum(PV.MontoCancelado))as
Deuda,SUM(PR.Importe)as TotalImporte
into ##SaldoPrestatario
from ##PrestamoVencido PV, ##PrestamoRiesgo PR
where PR.DocPrestamo=PV.DocPrestamo
group by PV.CodPrestatario
--indice de morosidad por comuniudad
select C.CodComunidad,C.Nombre,(sum(Deuda)/(2*sum(TotalImporte)))as
IndiceMorosidad
from ##SaldoPrestatario SP,Prestatario P,Comunidad C
where SP.CodPrestatario=P.CodPrestatario and
C.CodComunidad=P.CodComunidad
group by C.CodComunidad,C.Nombre,TotalImporte
drop table ##PrestamoVencido
drop table ##PrestamoRiesgo
drop table ##SaldoPrestatario
go
21.-Relacion de prestamos colocados por comunidad, para los años
2000,2001,2002 y 2004, con la siguiente informacion
R(CodComunidad,NombreComunidad,Ttoal2000,Total2001,total2002,tot
al2003)
SELECT c.CodComunidad,c.Nombre,sum(IIF(month(A.FechaPrestamo)= '01',
A.Importe, null)) as
Total2003, sum(IIF(YEAR(A.FechaPrestamo)= '2003', A.Importe, null))
as Total2003
FROM Prestamo A, Prestatario B, Comunidad C
WHERE (A.CodPrestatario=B.CodPrestatario) AND (B.CodComunidad =
C.CodComunidad)
GROUP BY C.CodComunidad, C.Nombre
go
22.-Relacion de prestamos colocados por comunidad, para los
meses del año 2003, con la siguiente informacion
R(CodComunidad,NombreComunidad,TotalFebrero,...,TotalDiciembre)
SELECT c.CodComunidad,c.Nombre,sum(IIF(month(A.FechaPrestamo)='01' and
year(A.FechaPrestamo)='2003', A.Importe, null)) as TotalEnero,
sum(IIF(month(A.FechaPrestamo)='02' and year(A.FechaPrestamo)='2003',
A.Importe, null)) as TotalFebrero,
sum(IIF(month(A.FechaPrestamo)='03' and year(A.FechaPrestamo)='2003',
A.Importe, null)) as TotalMarzo,
sum(IIF(month(A.FechaPrestamo)='04' and year(A.FechaPrestamo)='2003',
A.Importe, null)) as TotalAbril,
sum(IIF(month(A.FechaPrestamo)='05' and year(A.FechaPrestamo)='2003',
A.Importe, null)) as TotalMayo,
sum(IIF(month(A.FechaPrestamo)='06' and year(A.FechaPrestamo)='2003',
A.Importe, null)) as TotalJunio,
sum(IIF(month(A.FechaPrestamo)='07' and year(A.FechaPrestamo)='2003',
A.Importe, null)) as TotalJulio,
sum(IIF(month(A.FechaPrestamo)='08' and year(A.FechaPrestamo)='2003',
A.Importe, null)) as TotalAgosto,
sum(IIF(month(A.FechaPrestamo)='09' and year(A.FechaPrestamo)='2003',
A.Importe, null)) as TotalSeptiembre,
sum(IIF(month(A.FechaPrestamo)='10' and year(A.FechaPrestamo)='2003',
A.Importe, null)) as TotalOctubre,
sum(IIF(month(A.FechaPrestamo)='11' and year(A.FechaPrestamo)='2003',
A.Importe, null)) as TotalNovimbre,
sum(IIF(month(A.FechaPrestamo)='12' and year(A.FechaPrestamo)='2003',
A.Importe, null)) as TotalDiciembre
FROM Prestamo A, Prestatario B, Comunidad C
WHERE (A.CodPrestatario=B.CodPrestatario) AND (B.CodComunidad =
C.CodComunidad)
GROUP BY C.CodComunidad, C.Nombre
select sum(iif(YEAR(FechaPrestamo)='2003', Importe, null)) as
Total2003 from Prestamo
select * from Prestamo
go
Recommended