J'ai un schéma qui ressemble à ceci.
p>
Utilisation de la table d'enregistrements, interroger les données les plus récentes de la base de données pour chaque combinaison d'entités / accidents / mois (la table principale est la table principale) p>
3 Réponses :
Utilisez Row_Number () CODE> select * from
(
select *,row_number() over(partition by entid, accid, month order by timestamp desc) as rn
from recordings a inner join maintable b on a.logid=b.logid
)A where rn=1
Utilisez Row_Number () CODE> select * from
(
select m.*, row_number() over(partition by acctid,entid,month order by timestamp desc) rn
from maintable m join racordings r on m.logid=r.logid
) a where a.rn=1
Voici votre requête.
select * from
(select m.*, r.*, e.*, a.*, row_number() over(partition by a.AcctID, e.EntID, Month order by r.TimeStamp desc) row_num
from maintable m
inner join racordings r on m.LogID = r.LogID
inner join Entities e on e.EntID = m.EntID
inner join AccInfo a on a.AcctID = m.AcctID
) t1
where t1.row_num = 1