What SQL query returns the row wi开发者_如何转开发th the most recent Date and Time (Column B and C) for each unique Column A?
If C
is actually a datetime
column with date and time information set, you can do:
select a, max(c)
from table
group by a;
If B
is a date
column and C
is a time
column, then you need:
select a, max(convert(varchar(15), b) + ' ' + convert(varchar(15), c))
from table
group by a;
When I do:
select convert(varchar, getdate())
I get "Nov 19 2010 5:17PM", which doesn't help when finding the max().
I would spell out the style even if your database default doesn't have this problem. Something like:
select x.a,
cast(max(convert(varchar, x.b, 112)+' '+
convert(varchar, x.c, 108) as Datetime))
as maxDateTime
from table x
group by x.a
This produces "2010-11-19 17:20:29.000"
I know you are using Date type and Time type, but same idea.
精彩评论