I have two tables one of USERS and second of UserFiles just like below
UserTable
| UserId | Name | Phone | |
| 1 | New | [email protected] | 0909000 |
| 2 | Old | [email protected] | 0998099 |
UserFiles
| FileID(Primary) | UserID(Foreignkey) | FileName | DateCreated | Active |
| 1 | 1 | MyResume.Doc | 2/2/2016 | true |
| 2 | 1 | Image.JPG | 2/17/2016 | true |
| 3 | 1 | Cover.Docx | 2/20/2016 | true |
| 4 | 2 | Card.png | 1/1/2017 | true |
Now I have a gridview I want to show one File for each user I mean the latest file for this user According to datecreated for example here I have three files for userid=1 but i just want to show the Cover.Docx because it is latest one for this user according to datecreated Field.
I want this record how to do that
| RecordID | UserId | FileName | Phone | |
| 1 | 1 | Cover.Docx | [email protected] | 0909000 |
| 2 | 2 | card.png | [email protected] | 0998099 |
I want that record how to achived that

Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 12, 2016, 9:12 PM EDT
Vasanth NatarajanRank #3,352 · 429 reputationPosted Jun 12, 2016, 10:36 PM EDT
Nitin SontakkeRank #264 · 9.6K reputationPosted Jun 13, 2016, 4:35 AM EDT
Mark TaborRank #914 · 2.3K reputationPosted Jun 13, 2016, 7:30 AM EDT
ali tuncerRank #1,498 · 1.1K reputationPosted Jun 13, 2016, 8:22 PM EDT
try this :
select ufs.FileId,uf.userid,ufs.Filename,u.Email,u.Phone
from dbo.UserTable u
inner join (
select UserId ,Max(DateCreated) as CreateDate
from dbo.Userfiles
group by UserId) uf on u.UserId = uf.UserId
inner join dbo.Userfiles ufs on uf.userid = ufs.UserID and uf.CreateDate = ufs.DateCreated
Mark TaborRank #914 · 2.3K reputationPosted Jun 13, 2016, 8:50 PM EDT
Mark TaborRank #914 · 2.3K reputationPosted Jun 13, 2016, 9:03 PM EDT
theLizardRank #1,079 · 1.8K reputationPosted Jun 13, 2016, 9:39 PM EDT
FROM user
INNER JOIN userfiles
ON user.[id] = userfiles.[id1]