SlideShare una empresa de Scribd logo
1 de 17
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

Más contenido relacionado

La actualidad más candente (20)

Tipos de datos variables expresiones
Tipos de datos variables expresionesTipos de datos variables expresiones
Tipos de datos variables expresiones
 
Fundamentos de Programacion - Unidad 2 Algoritmos
Fundamentos de Programacion - Unidad 2 AlgoritmosFundamentos de Programacion - Unidad 2 Algoritmos
Fundamentos de Programacion - Unidad 2 Algoritmos
 
Ejercicios diagrama de flujo
Ejercicios diagrama de flujoEjercicios diagrama de flujo
Ejercicios diagrama de flujo
 
Metodología Incremental
Metodología IncrementalMetodología Incremental
Metodología Incremental
 
Expresiones regulares
Expresiones regularesExpresiones regulares
Expresiones regulares
 
Ejercicios resueltos de sql
Ejercicios resueltos de sqlEjercicios resueltos de sql
Ejercicios resueltos de sql
 
Ejemplos de llamadas al sistema
Ejemplos de llamadas al sistemaEjemplos de llamadas al sistema
Ejemplos de llamadas al sistema
 
Tipos De Datos
Tipos De DatosTipos De Datos
Tipos De Datos
 
Calculadora
CalculadoraCalculadora
Calculadora
 
Comandos importantes en c++
Comandos importantes en c++Comandos importantes en c++
Comandos importantes en c++
 
Programación 3: colas
Programación 3: colasProgramación 3: colas
Programación 3: colas
 
Programación funcional con haskell
Programación funcional con haskellProgramación funcional con haskell
Programación funcional con haskell
 
Recursividad
RecursividadRecursividad
Recursividad
 
Modelo entidad relacion
Modelo entidad relacionModelo entidad relacion
Modelo entidad relacion
 
Notación infija postfija
Notación infija postfijaNotación infija postfija
Notación infija postfija
 
Algoritmo
AlgoritmoAlgoritmo
Algoritmo
 
Ejercicios en java
Ejercicios en javaEjercicios en java
Ejercicios en java
 
Programas
Programas Programas
Programas
 
Analizador Léxico en C++
Analizador Léxico en C++Analizador Léxico en C++
Analizador Léxico en C++
 
Presentacion pilas lista y colas
Presentacion pilas lista y colas  Presentacion pilas lista y colas
Presentacion pilas lista y colas
 

Último

Clase 7 MECÁNICA DE FLUIDOS 2 INGENIERIA CIVIL
Clase 7 MECÁNICA DE FLUIDOS 2 INGENIERIA CIVILClase 7 MECÁNICA DE FLUIDOS 2 INGENIERIA CIVIL
Clase 7 MECÁNICA DE FLUIDOS 2 INGENIERIA CIVILProblemSolved
 
LA APLICACIÓN DE LAS PROPIEDADES TEXTUALES A LOS TEXTOS.pdf
LA APLICACIÓN DE LAS PROPIEDADES TEXTUALES A LOS TEXTOS.pdfLA APLICACIÓN DE LAS PROPIEDADES TEXTUALES A LOS TEXTOS.pdf
LA APLICACIÓN DE LAS PROPIEDADES TEXTUALES A LOS TEXTOS.pdfbcondort
 
Magnetismo y electromagnetismo principios
Magnetismo y electromagnetismo principiosMagnetismo y electromagnetismo principios
Magnetismo y electromagnetismo principiosMarceloQuisbert6
 
aCARGA y FUERZA UNI 19 marzo 2024-22.ppt
aCARGA y FUERZA UNI 19 marzo 2024-22.pptaCARGA y FUERZA UNI 19 marzo 2024-22.ppt
aCARGA y FUERZA UNI 19 marzo 2024-22.pptCRISTOFERSERGIOCANAL
 
osciloscopios Mediciones Electricas ingenieria.pdf
osciloscopios Mediciones Electricas ingenieria.pdfosciloscopios Mediciones Electricas ingenieria.pdf
osciloscopios Mediciones Electricas ingenieria.pdfIvanRetambay
 
CAPITULO 4 ANODIZADO DE ALUMINIO ,OBTENCION Y PROCESO
CAPITULO 4 ANODIZADO DE ALUMINIO ,OBTENCION Y PROCESOCAPITULO 4 ANODIZADO DE ALUMINIO ,OBTENCION Y PROCESO
CAPITULO 4 ANODIZADO DE ALUMINIO ,OBTENCION Y PROCESOLUISDAVIDVIZARRETARA
 
Falla de san andres y el gran cañon : enfoque integral
Falla de san andres y el gran cañon : enfoque integralFalla de san andres y el gran cañon : enfoque integral
Falla de san andres y el gran cañon : enfoque integralsantirangelcor
 
nomenclatura de equipo electrico en subestaciones
nomenclatura de equipo electrico en subestacionesnomenclatura de equipo electrico en subestaciones
nomenclatura de equipo electrico en subestacionesCarlosMeraz16
 
Controladores Lógicos Programables Usos y Ventajas
Controladores Lógicos Programables Usos y VentajasControladores Lógicos Programables Usos y Ventajas
Controladores Lógicos Programables Usos y Ventajasjuanprv
 
Obras paralizadas en el sector construcción
Obras paralizadas en el sector construcciónObras paralizadas en el sector construcción
Obras paralizadas en el sector construcciónXimenaFallaLecca1
 
clasificasion de vias arteriales , vias locales
clasificasion de vias arteriales , vias localesclasificasion de vias arteriales , vias locales
clasificasion de vias arteriales , vias localesMIGUELANGEL2658
 
01 MATERIALES AERONAUTICOS VARIOS clase 1.ppt
01 MATERIALES AERONAUTICOS VARIOS clase 1.ppt01 MATERIALES AERONAUTICOS VARIOS clase 1.ppt
01 MATERIALES AERONAUTICOS VARIOS clase 1.pptoscarvielma45
 
INTEGRALES TRIPLES CLASE TEORICA Y PRÁCTICA
INTEGRALES TRIPLES CLASE TEORICA Y PRÁCTICAINTEGRALES TRIPLES CLASE TEORICA Y PRÁCTICA
INTEGRALES TRIPLES CLASE TEORICA Y PRÁCTICAJOSLUISCALLATAENRIQU
 
Principales aportes de la carrera de William Edwards Deming
Principales aportes de la carrera de William Edwards DemingPrincipales aportes de la carrera de William Edwards Deming
Principales aportes de la carrera de William Edwards DemingKevinCabrera96
 
Quimica Raymond Chang 12va Edicion___pdf
Quimica Raymond Chang 12va Edicion___pdfQuimica Raymond Chang 12va Edicion___pdf
Quimica Raymond Chang 12va Edicion___pdfs7yl3dr4g0n01
 
tema05 estabilidad en barras mecanicas.pdf
tema05 estabilidad en barras mecanicas.pdftema05 estabilidad en barras mecanicas.pdf
tema05 estabilidad en barras mecanicas.pdfvictoralejandroayala2
 
desarrollodeproyectoss inge. industrial
desarrollodeproyectoss  inge. industrialdesarrollodeproyectoss  inge. industrial
desarrollodeproyectoss inge. industrialGibranDiaz7
 
ANALISIS Y DISEÑO POR VIENTO, DE EDIFICIOS ALTOS, SEGUN ASCE-2016, LAURA RAMIREZ
ANALISIS Y DISEÑO POR VIENTO, DE EDIFICIOS ALTOS, SEGUN ASCE-2016, LAURA RAMIREZANALISIS Y DISEÑO POR VIENTO, DE EDIFICIOS ALTOS, SEGUN ASCE-2016, LAURA RAMIREZ
ANALISIS Y DISEÑO POR VIENTO, DE EDIFICIOS ALTOS, SEGUN ASCE-2016, LAURA RAMIREZgustavoiashalom
 
UNIDAD 3 ELECTRODOS.pptx para biopotenciales
UNIDAD 3 ELECTRODOS.pptx para biopotencialesUNIDAD 3 ELECTRODOS.pptx para biopotenciales
UNIDAD 3 ELECTRODOS.pptx para biopotencialesElianaCceresTorrico
 
PPT ELABORARACION DE ADOBES 2023 (1).pdf
PPT ELABORARACION DE ADOBES 2023 (1).pdfPPT ELABORARACION DE ADOBES 2023 (1).pdf
PPT ELABORARACION DE ADOBES 2023 (1).pdfalexquispenieto2
 

Último (20)

Clase 7 MECÁNICA DE FLUIDOS 2 INGENIERIA CIVIL
Clase 7 MECÁNICA DE FLUIDOS 2 INGENIERIA CIVILClase 7 MECÁNICA DE FLUIDOS 2 INGENIERIA CIVIL
Clase 7 MECÁNICA DE FLUIDOS 2 INGENIERIA CIVIL
 
LA APLICACIÓN DE LAS PROPIEDADES TEXTUALES A LOS TEXTOS.pdf
LA APLICACIÓN DE LAS PROPIEDADES TEXTUALES A LOS TEXTOS.pdfLA APLICACIÓN DE LAS PROPIEDADES TEXTUALES A LOS TEXTOS.pdf
LA APLICACIÓN DE LAS PROPIEDADES TEXTUALES A LOS TEXTOS.pdf
 
Magnetismo y electromagnetismo principios
Magnetismo y electromagnetismo principiosMagnetismo y electromagnetismo principios
Magnetismo y electromagnetismo principios
 
aCARGA y FUERZA UNI 19 marzo 2024-22.ppt
aCARGA y FUERZA UNI 19 marzo 2024-22.pptaCARGA y FUERZA UNI 19 marzo 2024-22.ppt
aCARGA y FUERZA UNI 19 marzo 2024-22.ppt
 
osciloscopios Mediciones Electricas ingenieria.pdf
osciloscopios Mediciones Electricas ingenieria.pdfosciloscopios Mediciones Electricas ingenieria.pdf
osciloscopios Mediciones Electricas ingenieria.pdf
 
CAPITULO 4 ANODIZADO DE ALUMINIO ,OBTENCION Y PROCESO
CAPITULO 4 ANODIZADO DE ALUMINIO ,OBTENCION Y PROCESOCAPITULO 4 ANODIZADO DE ALUMINIO ,OBTENCION Y PROCESO
CAPITULO 4 ANODIZADO DE ALUMINIO ,OBTENCION Y PROCESO
 
Falla de san andres y el gran cañon : enfoque integral
Falla de san andres y el gran cañon : enfoque integralFalla de san andres y el gran cañon : enfoque integral
Falla de san andres y el gran cañon : enfoque integral
 
nomenclatura de equipo electrico en subestaciones
nomenclatura de equipo electrico en subestacionesnomenclatura de equipo electrico en subestaciones
nomenclatura de equipo electrico en subestaciones
 
Controladores Lógicos Programables Usos y Ventajas
Controladores Lógicos Programables Usos y VentajasControladores Lógicos Programables Usos y Ventajas
Controladores Lógicos Programables Usos y Ventajas
 
Obras paralizadas en el sector construcción
Obras paralizadas en el sector construcciónObras paralizadas en el sector construcción
Obras paralizadas en el sector construcción
 
clasificasion de vias arteriales , vias locales
clasificasion de vias arteriales , vias localesclasificasion de vias arteriales , vias locales
clasificasion de vias arteriales , vias locales
 
01 MATERIALES AERONAUTICOS VARIOS clase 1.ppt
01 MATERIALES AERONAUTICOS VARIOS clase 1.ppt01 MATERIALES AERONAUTICOS VARIOS clase 1.ppt
01 MATERIALES AERONAUTICOS VARIOS clase 1.ppt
 
INTEGRALES TRIPLES CLASE TEORICA Y PRÁCTICA
INTEGRALES TRIPLES CLASE TEORICA Y PRÁCTICAINTEGRALES TRIPLES CLASE TEORICA Y PRÁCTICA
INTEGRALES TRIPLES CLASE TEORICA Y PRÁCTICA
 
Principales aportes de la carrera de William Edwards Deming
Principales aportes de la carrera de William Edwards DemingPrincipales aportes de la carrera de William Edwards Deming
Principales aportes de la carrera de William Edwards Deming
 
Quimica Raymond Chang 12va Edicion___pdf
Quimica Raymond Chang 12va Edicion___pdfQuimica Raymond Chang 12va Edicion___pdf
Quimica Raymond Chang 12va Edicion___pdf
 
tema05 estabilidad en barras mecanicas.pdf
tema05 estabilidad en barras mecanicas.pdftema05 estabilidad en barras mecanicas.pdf
tema05 estabilidad en barras mecanicas.pdf
 
desarrollodeproyectoss inge. industrial
desarrollodeproyectoss  inge. industrialdesarrollodeproyectoss  inge. industrial
desarrollodeproyectoss inge. industrial
 
ANALISIS Y DISEÑO POR VIENTO, DE EDIFICIOS ALTOS, SEGUN ASCE-2016, LAURA RAMIREZ
ANALISIS Y DISEÑO POR VIENTO, DE EDIFICIOS ALTOS, SEGUN ASCE-2016, LAURA RAMIREZANALISIS Y DISEÑO POR VIENTO, DE EDIFICIOS ALTOS, SEGUN ASCE-2016, LAURA RAMIREZ
ANALISIS Y DISEÑO POR VIENTO, DE EDIFICIOS ALTOS, SEGUN ASCE-2016, LAURA RAMIREZ
 
UNIDAD 3 ELECTRODOS.pptx para biopotenciales
UNIDAD 3 ELECTRODOS.pptx para biopotencialesUNIDAD 3 ELECTRODOS.pptx para biopotenciales
UNIDAD 3 ELECTRODOS.pptx para biopotenciales
 
PPT ELABORARACION DE ADOBES 2023 (1).pdf
PPT ELABORARACION DE ADOBES 2023 (1).pdfPPT ELABORARACION DE ADOBES 2023 (1).pdf
PPT ELABORARACION DE ADOBES 2023 (1).pdf
 

ejercicios propuestos guia4 base de datos sql server 2012

  • 1. 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ú
  • 2. 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. 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. 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. 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
  • 6. 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
  • 7. 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
  • 8. --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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. --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
  • 17. 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