If you're using SQL Server 2012 and up, then just check
FIRST_VALUE() and LAST_VALUE() functions
http://www.mssqltips.com/sqlservertip/2640/sql-server-2012-functions--firstvalue-and-lastvalue/>This is probably an easy one but I just thought I would talk a little bit about it so to obtain the best way to do it.
>
>Presently, I have two select commands which goes like this:
>
>
>SELECT TOP 1 Invoice.ApprovedPersonName FROM Invoice WHERE Invoice.NoClient=Client.Numero ORDER BY Invoice.AddDate
>
>
>
>SELECT TOP 1 Invoice.ApprovedPersonName FROM Invoice WHERE Invoice.NoClient=Client.Numero ORDER BY Invoice.AddDate DESC
>
>
>I would say this can be done with one select command with the use of MIN(), MAX(). However, I am not sure if this would always be adequate:
>
>
>SELECT MIN(Invoice.ApprovedPersonName) AS FirstOne,MAX(Invoice.ApprovedPersonName) AS LastOne
> FROM Invoice WHERE Invoice.NoClient=Client.Numero ORDER BY Invoice.AddDate
>
>
>Is this about the proper way to do it?
If it's not broken, fix it until it is.
My Blog