In SQL Server I've always found it a pain to get the max rows for a dataset, I'm looking for a list of the methods to retrieve the max rows with some guidance on performance and maintainability.
Sample Table:
DECLARE @Test TABLE (ID INT IDENTITY(1,1), name VARCHAR(50),
dateOfBirth DATETIME, TaxNumber varchar(10))
INSERT INTO @Test (name, dateOfBirth, TaxNumber)
SELECT 'Fred', convert(datetime, '25/01/1976', 103), '123' UNION ALL
SELECT 'Bob', convert(datetime, '03/03/1976', 103), '234' UNION ALL
SELECT 'Jane', convert(datetime, '13/06/1996', 103), '345' UNION ALL
SELECT 'Fred', convert(datetime, '14/02/1982', 103), '456' UNION ALL
SELECT 'Bob', convert(datetime, '25/10/1983', 103), '567' UNION ALL
SELECT 'Jane', convert(datetime, '12/04/1995', 103), '678' UNION ALL
SELECT 'Fred', convert(datetime, '03/03/1976', 103), '789'
select * from @Test
Gives:
ID name dateOfBirth TaxNumber
----------- --------- ----------------------- ----------
1 Fred 1976-01-25 00:00:00.000 123
2 Bob 1976-03-03 00:00:00.000 234
3 Jane 1996-06-13 00:00:00.000 345
4 Fred 1982-02-14 00:00:00.000 456
5 Bob 1983-10-25 00:00:00.000 567
6 Jane 1995-04-12 00:00:00.000 678
7 Fred 1976-03-03 00:00:00.000 789
If I want to retrieve the oldest person (group by name) full details, what methods can I use?
Desired output:
ID name dateOfBirth TaxNumber
----------- --------- ----------------------- ----------
1 Fred 1976-01-25 00:00:00.000 123
2 Bob 1976-03-03 00:00:00.000 234
6 Jane 1995-04-12 00:00:00.000 678