I've been trying to understate the best way to write an MS SQL 2005 query which pretty much does the following...
select distinct col1, col2, col3
from table1
Basically I want to perform Distinct on col1 but I dont want Dististinc on Col2/3, I just want there values.
I understand its not possible to write the query this way as I think I read the Distinct is applied to the row and not the col?
Could anyone please point me in the right direction? I did try right joing the values back in but this didnt work as I had to specify the extra joined cols in the distinct select which in turn performed the distinct on these, i.e..
select distinct t1.col1, t2.col2, t3.col3
from table1 t1
right join (select col1, col2, col3 from table1) t2
on t1.col1 = t2.col1
Edited to explain better..
select distinct t1.Hostname, t2.IP, t2.ActionDateTime, t2.Action
from tblUserActions t1
right join (select Hostname, IP, ActionDateTime from tblUserActions) t2
on t1.Hostname = t2.Hostname
Basically this table is a list of thousands of user actions and im trying to list distinct on the Hostname so I should only receive say 10 rows as thats how many different Hostnames there are. Then based on these hostnames I want to also join the most recent record data to the rows returned, so I'd like to return:
Hostname, IP, ActionDateTime, Action
1 Host1, 165.123.123.1, 2012-06-14 02:07:08, Logon
2 Host2, 165.123.123.2, 2012-06-14 03:07:08, Logoff
3 Host3, 165.123.123.3, 2012-06-14 04:07:08, Logon
4 Host4, 165.123.123.4, 2012-06-14 05:07:08, Logoff
etc...
Any help/pointers would be great! Cheers.