Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > SQL
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 24-09-2018
abelg abelg is offline
Miembro
 
Registrado: jul 2004
Posts: 50
Poder: 20
abelg Va por buen camino
Una solución prudente creo seria esta.

SQL.
Código SQL [-]
Create Table ##Pagos (
  CI int,
  Pagos int,
  Fecha DateTime
)

insert into ##Pagos Values(123, 50, convert(datetime,'30/10/1996', 103))
insert into ##Pagos Values(123, 23, convert(datetime,'30/11/1996', 103))
insert into ##Pagos Values(123, 45, convert(datetime,'31/12/1996', 103))
insert into ##Pagos Values(436, 45, convert(datetime,'30/10/1996', 103))
insert into ##Pagos Values(436, 96, convert(datetime,'30/11/1996', 103))
insert into ##Pagos Values(436, 87, convert(datetime,'31/12/1996', 103))
insert into ##Pagos Values(576, 63, convert(datetime,'30/10/1996', 103))
insert into ##Pagos Values(576, 74, convert(datetime,'30/11/1996', 103))


Select p.*
from ##Pagos p
inner join (Select CI, Max(Fecha) MaxFecha 
      From ##Pagos tp Group by tp.CI) pp on pp.CI = p.CI and p.Fecha = pp.MaxFecha


Drop Table ##Pagos


Se que es un post pasado pero puede servir a alguien.

Para el Penultimo.

Código SQL [-]
Create Table ##Pagos (
  CI int,
  Pagos int,
  Fecha DateTime
)

insert into ##Pagos Values(123, 50, convert(datetime,'30/10/1996', 103))
insert into ##Pagos Values(123, 23, convert(datetime,'30/11/1996', 103))
insert into ##Pagos Values(123, 45, convert(datetime,'31/12/1996', 103))
insert into ##Pagos Values(436, 45, convert(datetime,'30/10/1996', 103))
insert into ##Pagos Values(436, 96, convert(datetime,'30/11/1996', 103))
insert into ##Pagos Values(436, 87, convert(datetime,'31/12/1996', 103))
insert into ##Pagos Values(576, 63, convert(datetime,'30/10/1996', 103))
insert into ##Pagos Values(576, 74, convert(datetime,'30/11/1996', 103))


Select *, CAST(0 as int) IsLast
into #NewPagos
from ##Pagos


update np set np.IsLast = 2
from #NewPagos np
inner join (Select CI, Max(Fecha) MaxFecha
      from #NewPagos
      Group by CI) p on p.CI = np.CI and p.MaxFecha = np.Fecha

update np set np.IsLast = 1
from #NewPagos np
inner join (Select CI, Max(Fecha) MaxFecha
      from #NewPagos
      Where IsLast != 2
      Group by CI) p on p.CI = np.CI and p.MaxFecha = np.Fecha

--Select CI, Pagos, Fecha
--from #NewPagos

Select CI, Pagos, Fecha
from #NewPagos
Where IsLast = 1


Drop Table ##Pagos
Drop table #NewPagos

Última edición por abelg fecha: 24-09-2018 a las 21:58:54.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


La franja horaria es GMT +2. Ahora son las 01:42:35.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi