如果查询结果 sql 语句

来源:百度知道 编辑:UC知道 时间:2024/06/18 04:05:14
name , day
a,2008-8-1
a,2008-8-2
a,...
b,2008-7-29
b,2008-8-2
b,...
c,2008-8-2
c,2008-8-3
c,...
如何获得a , b , c 最早日期
d,...
e,...
f,...
然后选 top 10 要按day 按大至小排列

select name,min(day)
from table名字
group by name
注意,请保证day是时间类型,而不是字符串类型,否则可能无法得到正确的结果(牵扯到什么字符集之类的,结果很难说)

1.day 是datetime型
select name,day=min(day) from tablename group by name
2.Day 是字符型
select name,day=min(convert(datetime,day,120)) from tablename group by name

=============
select top 10 from (
select name,day=min(day) from tablename group by name
) order by day desc