Ok, I know I'm going to feel really silly when someone answers this because it should be very basic, but I'm at a loss at this point on how to select a record based on a min/max value. I realize I can use a Having clause, but it doesn't produce what I want.
Take the following data:
Create Table Customer (CustomerId int primary key, DateofBirth int);
Insert Into Customer (CustomerId, DateOfBirth)
Values (1, 19010801);
Insert Into Customer (CustomerId, DateOfBirth)
Values (2, 19100101);
Insert Into Customer (CustomerId, DateOfBirth)
Values (3, 19201201);
Select * from Customer
Results:
CustomerId DateOfBirth
1 19010801
2 19100101
3 19201201
Ok, so if I want to know the customerid of the person with the min or max date of birth, how would I go about it?
I can write:
Select customerid, Min(dateofbirth), Max(dateofbirth)
From Customer
Group by customerid
but it just yields all records grouped by customerid. What if I wanted to see just the customerid and the dateofbirth of the min or max?
Thanks!



LinkBack URL
About LinkBacks
Reply With Quote
Thanks Wolffy..as usual

Bookmarks