How does one get the primary key of the row that is joined in via a
group by aggregate clause when the aggregate is not performed on the
primary key?
For example,
Person table
(
PersonID int,
FirstName varchar(50)
LastName varchar(50)
)
Visit table
(
VisitID int,
PersonID int,
VisitDate datetime
)
These are simplified versions of my tables. I'm trying to create a
view that gets the first time each person Visited:
select p.PersonID,
min(v.VisitDate)
from Visit v
join Person p on p.PersonID = v.PersonID
group by p.PersonID
The problem is that I would like to return the VisitID in the
resultset, but when I do it expands the query since I have to also put
it in the group by clause.
What are the different ways to achieve this?
Subqueries?
Only return the date and then join off of date on the outside?
Neither of these seem too entising...
Thanks in advance for any help.
-Dave