我有这个结构
table idc (numId,idInt, IdAffiliate)
table glob (idInt, IdAtt)
table gratt(IdAtt, dtRomp)
Table update(IdAffiliate, dateUpdate)
制作这个select语句会给我这个:
SELECT
NumId,
dateUpdate,
DtRomp,
Idc.IdFiliale
FROM Idc inner join glob on glob .IdInt = Idc.IdInt
inner join Grat on Glob.IdAtt = Grat.IdAtt
inner join update on update.IdAffiliate = Idc.IdAffiliate
where NumId = 9976666
会给我这个:
NumId DtUpdate DtRomp filiale
9976666 01/05/2005 11/07/2006 27
9976666 01/05/2005 03/07/2008 27
9976666 01/05/2005 24/06/2010 27
9976666 01/05/2006 11/07/2006 27
9976666 01/05/2006 03/07/2008 27
9976666 01/05/2006 24/06/2010 27
我试图这样做: 选择与DtRomp最接近的dtUpdqte并且它不如它
最诚挚的问候
我一直在尝试,但还没有解决方案。
答案 0 :(得分:0)
您可以使用row_number()
:
select NumId, dateUpdate, DtRomp, Idc.IdFiliale
from (SELECT NumId, dateUpdate, DtRomp, Idc.IdFiliale,
row_number() over (partition by NumID, DTRomp order by DTRomp desc) as seqnum
FROM Idc inner join glob on glob .IdInt = Idc.IdInt
inner join Grat on Glob.IdAtt = Grat.IdAtt
inner join update on update.IdAffiliate = Idc.IdAffiliate
where NumId = 9976666 and dateUpdate < DTRomp
) t
where seqnum = 1;
答案 1 :(得分:0)
尝试使用datediff
功能,如下所示:
SELECT
NumId,
dateUpdate,
DtRomp,
Idc.IdFiliale
FROM Idc inner join glob on glob .IdInt = Idc.IdInt
inner join Grat on Glob.IdAtt = Grat.IdAtt
inner join update on update.IdAffiliate = Idc.IdAffiliate
where NumId = 9976666
and datediff(day,dateUpdate,DtRomp) = (
SELECT
min(datediff(day,dateUpdate,DtRomp))
FROM Idc inner join glob on glob .IdInt = Idc.IdInt
inner join Grat on Glob.IdAtt = Grat.IdAtt
inner join update on update.IdAffiliate = Idc.IdAffiliate
where NumId = 9976666
)
答案 2 :(得分:0)
它与此一起工作!!!!!
SELECT
NumId,
dateUpdate,
DtRomp,
Idc.IdFiliale
FROM Idc inner join glob on glob .IdInt = Idc.IdInt
inner join Grat on Glob.IdAtt = Grat.IdAtt
inner join update on update.IdAffiliate = Idc.IdAffiliate
where NumId = 9976666
and datediff(day,dateUpdate,DtRomp) = (
SELECT
min(datediff(day,dateUpdate,DtRomp))
FROM Idc ainner join glob b on a.IdInt = c.IdInt
inner join Grat con b.IdAtt = c.IdAtt
inner join update d on d.IdAffiliate = Idc.IdAffiliate
where NumId = 9976666 and Idc.IdAffiliate = a.IdAffiliate and Grat.DtRompu = c.DtRompu and Grat.DtRompu>DtDebValidite
)
此致