I have a table with the following columns:
ConnectionId INT - primary key, identity,
ServerId INT,
ClientId INT,
Time DATETIME,
ConnectionType INT
How do I define an optimal view which returns all combinations of ServerId and ClientId and time and type of LAST connection for each combination?
Assuming you are using a database that supports ranking functions (such as SQL Server 2005+, Oracle, Postgres, DB2, Teradata . . .), you can do this as follows:
I don’t know what you mean by “optimal view”. If you want this to work better, create a three-part index on ServerId, ClientId, and time.