I have a table in sql server. It has a pid field used as a primary key and a field called deviceid that has a device name. example:
pid deviceid field3 field4
1 Device1 test test
2 Device2 test2 test2
3 Device1 test3 test3
4 Device2 test4 test4
For a query, i need select * from the table where the pid is the max per device. Example result:
pid deviceid field3 field4
3 Device1 test3 test3
4 Device2 test4 test4
Im not sure how do do this. Any one have any ideas?
The closest i got was:
Select MAX(pid) from TheTable Group By deviceid;
This works but it only gives me the max pid per device in the results and i need all the field for that record. Adding in the other fields to the select clause resulted in errors saying that the fields need to be listed in the group by clause... Anyone know how to do this?