除日期/时间列外,还有几列的表。如何查询和避免重复的日期戳?

时间:2018-07-23 18:25:49

标签: sql sql-server datetime

我在SQL Sever中有一个表,除其他列外,还具有以下日期/时间戳列。我如何查询该表以避免在结果中出现重复的日期?我不在乎查询返回哪个时间戳。我只想要唯一的日期,例如仅一个05/05/2018,并且如果可能的话,其相应的时间戳和其相应的RPM值。类似地,一个06/11/2018及其相应的rpm值和时间戳等等........

RPM         Date-time
7932    06/04/2018 16:30:01 +00:00
8314    06/11/2018 16:30:00 +00:00
8748    06/12/2018 20:52:10 +00:00
8067    06/05/2018 20:15:00 +00:00
8063    06/05/2018 12:45:34 +00:00
8067    06/05/2018 20:15:00 +00:00
8070    06/07/2018 12:02:16 +00:00
8144    06/08/2018 20:30:00 +00:00
8144    06/08/2018 20:30:00 +00:00
8314    06/11/2018 16:30:00 +00:00
8314    06/11/2018 16:30:00 +00:00
8314    06/11/2018 16:30:00 +00:00
8748    06/12/2018 20:52:10 +00:00
8748    06/12/2018 20:52:10 +00:00
9046    06/14/2018 12:14:46 +00:00
9192    06/15/2018 11:13:23 +00:00
9274    06/15/2018 20:30:04 +00:00
9293    06/20/2018 18:47:10 +00:00
9562    06/22/2018 12:30:00 +00:00
10141   06/25/2018 18:26:13 +00:00
10443   06/28/2018 18:35:21 +00:00
10500   06/29/2018 11:14:42 +00:00
10500   06/29/2018 11:14:42 +00:00
10500   06/29/2018 11:14:42 +00:00
10506   06/29/2018 15:45:22 +00:00
10506   06/29/2018 15:45:22 +00:00
10506   06/29/2018 15:45:22 +00:00
10526   07/05/2018 14:14:49 +00:00
10526   07/05/2018 14:14:49 +00:00
10526   07/05/2018 14:14:49 +00:00
10526   07/05/2018 14:14:49 +00:00

4 个答案:

答案 0 :(得分:1)

我认为与众不同应该起作用:

select distinct RPM, Date-time
from table t;

但是,我假设您的表也有其他列,那么您可以使用row_number()函数:

select t.*
from (select *, row_number() over (partition by rpm order by date-time) as seq
      from table
     ) t
where seq = 1;

答案 1 :(得分:1)

如果您需要“日期时间”的RPM和日期部分不同,则可以通过以下方式实现:

SELECT Distinct RPM, CONVERT(VARCHAR(10),Date-time,111) FROM Table1

答案 2 :(得分:1)

您需要进行投射和分组:

select rpm,cast([Date-time] as date) as _date
from table t
group by rpm,cast([Date-time] as date)

答案 3 :(得分:1)

您可以按RPM分组,但随后必须为返回的每个列提供聚合函数(或按它们分组):

例如

SELECT RPM, MIN(Date-Time) FROM TableName GROUP BY RPM