返回不同的日期 [英] Returning Distinct Dates

查看:83
本文介绍了返回不同的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早晨

我正在尝试通过唯一标识符返回结果的不同日期。

I am trying to return the distinct dates of an outcome by a unique identifer.

例如:

ID|Date
1 | 2011-10-01 23:57:59
1 | 2011-10-01 23:58:59
1 | 2010-10-01 23:59:59
2 | 2010-09-01 23:59:59
2 | 2010-09-01 23:58:29
3 | 2010-09-01 23:58:39
3 | 2010-10-01 23:59:14
3 | 2010-10-01 23:59:36

时间并不仅仅取决于日期。因此,例如,在ID 1上,我无法对ID进行区分,因为那只会返回我的一个日期。所以我想返回:

The times are not important just the dates. So for example on ID 1 I can't do a distinct on the ID as that would return only one of my dates. So I would want to return:

1|2011-10-01
1|2010-10-01

我尝试了以下查询:

Drop Table #Temp

select Distinct DateAdd(dd, DateDiff(DD,0, Date),0) as DateOnly
,ID
Into #Temp
From Table

Select Distinct (Date)
,ID
From #Temp

但是我得到以下结果:

ID|Date
1 | 2011-10-01 00:00:00
1 | 2011-10-01 00:00:00
1 | 2010-10-01 00:00:00

我是SQL的新手,所以我很抱歉犯了一个明显的错误。到目前为止,我已经通过搜索先前提出的问题来解决问题。

I'm new to SQL so apologies I may have made a glaring mistake. I have got so far by searching through the previously asked questions.

一如既往,任何帮助和指针都将不胜感激。

As always any help and pointers is greatly appreciated.

推荐答案

您可以使用T-SQL convert 函数提取日期。

You can use the T-SQL convert function to extract the Date.

尝试

CONVERT(char(10), GetDate(),126)

所以,在您的情况下,

Drop Table #Temp

select Distinct CONVERT(char(10), DateAdd(dd, DateDiff(DD,0, Date),0), 126) as DateOnly
,ID
Into #Temp
From Table

Select Distinct (Date)
,ID
From #Temp 

更多信息:获取SQL Server日期时间字段的日期部分

希望这会有所帮助!

这篇关于返回不同的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆