Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
First and last based on same SQL
Message
From
10/10/2014 02:33:45
Walter Meester
HoogkarspelNetherlands
 
 
To
09/10/2014 15:30:56
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2012
Application:
Web
Miscellaneous
Thread ID:
01609079
Message ID:
01609112
Views:
73
This message has been marked as the solution to the initial question of the thread.
Michel,

From what I sense, you want to have the fist and latest person that approved any invoice for every client? Correct?

I think it can be done very simple:
SELECT *,
   (SELECT TOP 1 ApprovedPersonName FROM Invoices WHERE NoClient = Client.Numero ORDER BY AddDate ) as firstperson,
   (SELECT TOP 1 ApprovedPersonName FROM Invoices WHERE NoClient = Client.Numero ORDER BY AddDate DESC) as Lastperson,
   FROM Client
Walter,


>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?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform