Sie sind auf Seite 1von 5

Nombre: Carlos Enrique Paucar Farfan Cdigo: 103174 Docente: Ing.

Arturo Rozas Huacho Curso : Laboratorio de Base de Datos I Carrera: Ing. Informatica y de Sistemas. _________________________________________________________________________________________ /*Ejercicio1:R(PrestamosCanceladosdeUndeterminadoPrestatario)*/ select P.DocPrestamo , P.CodPrestatario , (P.Importe-Sum(ISNULL(A.Importe,0))) as Saldo from Prestamo P left outer join Amortizacion A on A.DocPrestamo = P.DocPrestamo group by P.DocPrestamo ,P.CodPrestatario,P.Importe having (P.Importe - Sum(Isnull(A.Importe,0))) = 0 /*Ejercicio2:R(PrestamosEfectuadosPorLosPrestatariosDeUnaDeterminadaComunidad)*/ select C.Nombre as Comunidad, B.CodPrestatario, P.DocPrestamo from Comunidad C, Prestatario B, Prestamo P where (C.CodComunidad=B.CodComunidad) and (B.CodPrestatario = P.CodPrestatario) Group by P.DocPrestamo, B.CodPrestatario,C.Nombre /*Ejercicio3:R(PrestatariosQueHayanEfectuadoMasDe5Prestamos)*/ Select B.CodPrestatario,count(B.DocPrestamo) as NroPrestamos from Prestamo B Group by B.CodPrestatario having count (B.DocPrestamo) > 5 /*Ejercicio4:R(PrestatariosMorosos)*/ --Seleccionar a los prestatarios morosos Select P.CodPrestatario,P.DocPrestamo,(P.Importe-Sum(ISNULL(A.Importe,0))) as Saldo --Conbinando la tabla Prestamo y Amortizacion from Prestamo P left outer join Amortizacion A -- en base al DocPrestamo on A.DocPrestamo = P.DocPrestamo group by P.CodPrestatario,p.DocPrestamo,P.Importe having P.Importe-Sum(ISNULL(A.Importe,0)) <> 0 /*Ejercicio5:R(5ComunidadesMayorNroPrestatarios)*/ --Seleccionando las primeras 5 Comunidades evaluado por NroPrestatarios Select top 5 P.CodComunidad,Count(P.CodPrestatario) as NroPrestatarios --Juntando la tabla Prestamo y Prestatario from Prestamo B Join Prestatario P --en base al CodPrestatario on B.CodPrestatario=P.CodPrestatario --agrupado por Comunidad Group by P.CodComunidad Order by NroPrestatarios desc --6. Relacion de comunidades cuyos prestatarios que aun tienen saldos, no hayan efectuado ninguna -amortizacion en lo que va del anio 2007 --Obtenemos la relacion de Prestatarios y Prestamos que aun no se cancelaron: select P.CodPrestatario, P.DocPrestamo, P.Importe, SUM(ISNULL(A.Importe, 0)) as TotalCancelado into #TMP_Deudores from Prestamo P left outer join Amortizacion A on (P.DocPrestamo = A.DocPrestamo) group by P.CodPrestatario, P.DocPrestamo, P.Importe having (SUM(ISNULL(A.Importe, 0)) < P.Importe) select * from #TMP_Deudores --Obtenemos la relacion de Prestatarios que no amortizaron el 2007

select P.CodPrestatario, A.DocCancelacion, A.FechaCancelacion into #TMP_SinAmortizar2007 from Prestamo P inner join Amortizacion A on (P.DocPrestamo = A.DocPrestamo) where not (A.FechaCancelacion like '%2007%') select * from #TMP_SinAmortizar2007 --Obtenemos la relacion de prestatarios requeridos select distinct T1.CodPrestatario into #TMP_R from #TMP_Deudores T1 inner join #TMP_SinAmortizar2007 T2 on (T1.CodPrestatario = T2.CodPrestatario) select * from #TMP_R --Obtenemmos la lista de comunidades a la que pertenecen los prestatarios select C.CodComunidad, C.Nombre from #TMP_R A, Prestatario B, Comunidad C where(A.CodPrestatario = B.CodPrestatario) and (B.CodComunidad = C.CodComunidad) -------------------------------------------------------------------------------------------

select P.CodPrestatario, P.DocPrestamo, P.Importe , A.DocCancelacion, A.FechaCancelacion from Prestamo P left outer join Amortizacion A on (P.DocPrestamo = A.DocPrestamo) where not (A.FechaCancelacion like '%2007%')

select * from Amortizacion where (FechaCancelacion like '%2008%')

select CodComunidad, Nombre from Comunidad C where exists ( select C.CodComunidad, C.Nombre, 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)) inner join Comunidad C on (B.CodComunidad = C.CodComunidad) where not (A.FechaCancelacion like '%/2007%') group by C.CodComunidad, C.Nombre, P.Importe having ((P.Importe - SUM(ISNULL(A.Importe, 0)))) > 0 ) --7. Relacion de comunidades que no tengan prestatarios morosos

--Obtenemos la relacion de prestatarios morosos

select P.CodPrestatario, P.DocPrestamo, P.FechaVencimiento, P.Importe, SUM(ISNULL(A.Importe, 0)) as TotalCancelado into #Morosos from Prestamo P left outer join Amortizacion A on (P.DocPrestamo = A.DocPrestamo) group by P.CodPrestatario, P.DocPrestamo, P.Importe, P.FechaVencimiento having (P.FechaVencimiento <= GETDATE()) and (P.Importe > SUM(ISNULL(A.Importe, 0))) select * from #Morosos drop table #Morosos --Seleccionamos los Prestatarios que no se encuentren en la lista de morosos select CodPrestatario into #PrestatariosAldia from Prestatario where CodPrestatario not in (select M.CodPrestatario from #Morosos M) --Obtenemos las comunidades a la que pertenecen los prestatarios al dia select distinct C.CodComunidad, C.Nombre from #PrestatariosAlDia A, Prestatario B, Comunidad C where(A.CodPrestatario = B.CodPrestatario) and (B.CodComunidad = C.CodComunidad)

--------------------------------------------------------------------------------------select P.CodPrestatario, P.DocPrestamo, P.FechaVencimiento, P.Importe, SUM(ISNULL(A.Importe, 0)) as TotalCancelado into #PrestatariosAlDia from Prestamo P left outer join Amortizacion A on (P.DocPrestamo = A.DocPrestamo) group by P.CodPrestatario, P.DocPrestamo, P.Importe, P.FechaVencimiento having (P.FechaVencimiento >= GETDATE()) or (P.Importe <= SUM(ISNULL(A.Importe, 0))) select * from #PrestatariosAlDia --drop table #PrestatariosAlDia --Obtenemmos la lista de comunidades a la que pertenecen los prestatarios al dia select distinct C.CodComunidad, C.Nombre from #PrestatariosAlDia A, Prestatario B, Comunidad C where(A.CodPrestatario = B.CodPrestatario) and (B.CodComunidad = C.CodComunidad) --8. Relacion de comunidades que tengan prestatarios morosos --Obtenemos la relacion de prestatarios morosos select P.CodPrestatario, P.DocPrestamo, P.FechaVencimiento, P.Importe, SUM(ISNULL(A.Importe, 0)) as TotalCancelado into #PrestatariosMorosos from Prestamo P left outer join Amortizacion A on (P.DocPrestamo = A.DocPrestamo)

group by P.CodPrestatario, P.DocPrestamo, P.Importe, P.FechaVencimiento having (P.FechaVencimiento <= GETDATE()) and (P.Importe > SUM(ISNULL(A.Importe, 0))) select * from #PrestatariosMorosos --drop table #PrestatariosMorosos --Obtenemmos la lista de comunidades a la que pertenecen los prestatarios morosos select distinct C.CodComunidad, C.Nombre from #PrestatariosMorosos A, Prestatario B, Comunidad C where(A.CodPrestatario = B.CodPrestatario) and (B.CodComunidad = C.CodComunidad)

--9. Relacion de comunidades con 3 de sus prestatarios mas importantes -(los prestatarios mas importantes son los que han obtenido mayor numero de prestamos) 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 /*Ejercicio12:R(PrestatariosQueCancelaronDeUnaSola)*/ Select P.CodPrestatario ,Count(A.DocPrestamo) as NroAmortizaciones from Amortizacion A left outer join Prestamo P on A.DocPrestamo = P.DocPrestamo Group by P.CodPrestatario having Count(A.DocPrestamo) = 1 /*Ejercicio13:R(OficialesCreditoEstrellaDeCadaMes2003)*/ Select DATEPART (mm , Pr.FechaPrestamo) AS N'Month',OC.Nombres, Pr.CodOficial, Count(Pr.DocPrestamo) as NroPrestamo into #T1 --Juntar las dos tablas From Prestamo Pr inner join Oficial_Credito OC --en base al CodOficial on Pr.CodOficial = OC.CodOficial --Calculando los OficialesCredito y sus NroPrestamos efectuados GROUP BY DATEPART(mm , Pr.FechaPrestamo), Pr.CodOficial, Pr.CodOficial, OC.Nombres Order by DATEPART(mm , Pr.FechaPrestamo) Select * from Oficial_Credito /*Select */ Select T.Month, Max(T.NroPrestamo) as Maximo into #T2 From #T1 T Select #T1.Month,#T2.Maximo, #T1.CodOficial,#T1.Nombres, #T1.NroPrestamo from #T2 inner join #T1 on #T2.Maximo = #T1.NroPrestamo GROUP BY #T1.Month,#T2.Maximo,#T1.CodOficial, #T1.Nombres,#T1.NroPrestamo Order by #T1.Month --Ejercicio14:R(OficialesCreditoQueNoColocaronPrestamo,en algun mes del ano 2003,como max 1) Select datepart (month,FechaPrestamo)as Mes,OC.CodOficial,OC.Nombres,Count (P.DocPrestamo) as NroPrestamo

from Oficial_Credito OC left outer join Prestamo P on OC.CodOficial = P.CodOficial Group by datepart (month,P.FechaPrestamo),OC.CodOficial, OC.Nombres,P.FechaPrestamo having Count (P.DocPrestamo) <= 1 and datepart (yyyy,P.FechaPrestamo) = '2003' Order by datepart (month,FechaPrestamo) --Ejercicio15:

Das könnte Ihnen auch gefallen