Join to the first row in other table - using CROSS APPLY:
SELECT aId, aInt, Adress, AbitInt
FROM dbo.aaa
SELECT bId, DateCreated, aId
FROM dbo.bbb
ORDER BY aId, DateCreated
-- for each row in aaa - join to the earliest row in bbb
SELECT a.aId, a.aInt, a.Adress, a.AbitInt, bb.bId, bb.DateCreated
FROM dbo.aaa a
CROSS APPLY
( SELECT TOP 1 bId, DateCreated, aId
FROM dbo.bbb b
WHERE b.aId = a.aId
ORDER BY aId, DateCreated
) bb
No comments:
Post a Comment