I have a list of players with skill values as at various dates. These records only exist when and where a skill changed.In order to graph the historical changes of these player skills I wish to create a query that will list the latest skill value per player on specific dates (eg. every Sunday) - Meaning that if there is no value on the date for wanted Graph Date, then take the highest available date that is prior to the GraphDateWanted.My tables are:ArchivePlayers.PlayerIDCode (int)ArchivePlayers.Firstname (nvarchar(50))ArchivePlayers.LastName (nvarchar(50))ArchivePlayerSkillsLong.PlayerIDCode (int)ArchivePlayerSkillsLong.Batting(int)ArchivePlayerSkillsLong.Bowling(int)ArchivePlayerSkillsLong.Date (datetime)GraphDateWanted.Date (datetime)SELECT ArchivePlayerSkillsLong.PlayersID, ArchivePlayers.FirstName, ArchivePlayers.LastName, MAX(ArchivePlayerSkillsLong.Date) AS MaxDate, GraphDateWanted.Date AS GraphDateFROM ArchivePlayerSkillsLong INNER JOIN ArchivePlayers ON ArchivePlayerSkillsLong.ArchivePlayersID = ArchivePlayers.ArchivePlayersID LEFT OUTER JOIN GraphDateWanted ON ArchivePlayerSkillsLong.Date <= GraphDateWanted.DateGROUP BY ArchivePlayerSkillsLong.PlayersID, GraphDateWanted.Date, ArchivePlayers.FirstName, ArchivePlayers.LastNameHAVING (GraphDateWanted.Date IS NOT NULL)
This code gives me the latest skills date < = graph date wanted. It seems to be correct on all the samples I have studied from it.But, as soon as I select the batting or bowling .. the data is incorrect. I then get duplicate records for graph dates - not just the max available date prior to the Wanted Graph Date. I do not know how to filter (or do the join) so that I do not get those duplicates. Can someone help, please?