I have a table of login events coming from Active Directory. One type of these events are machine logins, which include the IP address of the machine doing the login. This is handy, since it provides a timestamped way to determine what machine was on what IP at a given time. I'm trying to construct a query that'll give me a timestamped list of when a machine first logged in to an IP (thank you DHCP, IP is variable).
The query that just returns the list if IP addresses that machine has held is simple.
SELECT DISTINCT IP
FROM EventStream
WHERE (Machine='$Machine')
I know 'select distinct' is a non-optimal query, which is why I'm looking for something better. This probably includes sub-queries, of which I know very little. Not providing 'Distinct' in this case returns a table with up to 2000 rows, so a lot of data is being selected out and not used.
What I would really like is some way to phrase a query such that I get a time-stamped list of when a machine first showed up on an IP address. I can fake it in code by iterating this query over the results of the first:
SELECT TOP 1 DateTime
FROM EventStream
WHERE (Machine='$Machine' and IP='$IP')
ORDER BY DateTime
I'm pretty sure these two can be combined into a grand-unified-query. Is this possible, or should I stick with application logic to provide what I'm looking for?