如何选择不同的日期,不包括当天的时间

时间:2014-02-17 02:45:10

标签: mysql sql sql-server

这是我的表

+---------------------+
| access_time         |
+---------------------+
| 2014-02-17 12:00:00 |
| 2014-02-15 12:00:00 |
| 2014-02-15 12:00:00 |
| 2014-02-15 12:00:00 |
| 2014-02-15 11:00:00 |
| 2014-02-14 02:00:00 |
| 2014-02-13 18:00:00 |
| 2014-02-13 12:50:05 |
| 2014-02-13 12:48:57 |
| 2014-02-13 11:57:24 |
+---------------------+

我希望有这样的输出:

+---------------------+
| access_time         |
+---------------------+
| 2014-02-17 12:00:00 |
| 2014-02-15 12:00:00 |
| 2014-02-14 02:00:00 |
| 2014-02-13 18:00:00 |
+---------------------+

具有那种输出的SQL命令是什么?

4 个答案:

答案 0 :(得分:1)

如果你想每天任意一个时间,你可以这样做:

select access_time
from table t
group by date(access_time);

更典型的是,您可以选择您想要的次数,并执行以下操作:

select min(access_time)
from table t
group by date(access_time);

答案 1 :(得分:1)

对于SQL Server 2008或更新版本,我建议:

select cast(access_time as date)
from t
group by cast(access_time as date)

答案 2 :(得分:0)

我认为这应该有用

With cte as (Select *,row_number ()over(partion by convert(date,access_time) 
             order by access_time desc) rn from <yourtable>)
Select * from <yourtable> where rn=1

答案 3 :(得分:0)

试试这个

  

从tablename group by date

中选择distinct cast(access_time as Date)