Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to write this SQL Statement?

Author  Topic 

mutlyp
Starting Member

20 Posts

Posted - 2014-04-09 : 15:46:35
I have a table that holds an ID, Location and DateTime.
The Datetime is in the format of yyyyMMddHHmmsec100thsec
So there would be records like this:

ID Location DateTime
01 Home 20140301115522003
01 Home 20140302103311010
01 Home 20140311085725011
04 Away 20140415163322055
04 Away 20140415175522053

After I run the Query I want it to show:
ID Location DateTime
01 Home 20140311085725011
04 Away 20140415175522053

So it grabs the MAX Datetime of the group of IDs.

What is the query to accomplish this???

Please help.

Thank you


imex
Starting Member

1 Post

Posted - 2014-04-09 : 17:12:23
Try something like this:

select [ID], [Location], max([DateTime]) as [DateTime]
from MyTable
group by [ID], [Location]


Hope this helps.

http://www.imoveisemexposicao.com.br
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-04-10 : 04:27:07
Also you need to store datetime values with proper DATETIME datatype

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -