-8

我正在尝试使用此查询获取日期时差:

SELECT BASESCORE, 
       Max(CONVERT(DATETIME, '')) - Min(CONVERT(DATETIME, '')) AS datediff 
FROM   log 
GROUP  BY BASESCORE 
ORDER  BY BASESCORE 

它工作正常,但它给我的输出是错误的:

   1900-01-01 00:00:00.000
   1900-01-01 00:00:00.000
   1900-01-01 00:00:00.000
   1900-01-01 00:00:00.000
   1900-01-01 00:00:00.000
                    ......

我怎样才能从上面的查询中得到正确的结果!

4

5 回答 5

3

正如@Nithesh 所说,尝试使用DATEDIFF-

DECLARE @temp TABLE(
       [datetime] DATETIME
     , basescore INT
)

INSERT INTO @temp
VALUES
     ('20130320 03:17:43', 1),
     ('20130320 03:17:43', 2),
     ('20130320 04:17:43', 1),
     ('20130320 03:20:43', 2)

SELECT
       basescore
     , [datediff] = CONVERT(
                         VARCHAR(10), 
                         DATEADD(ms, 
                         DATEDIFF([second], MIN([datetime]), MAX([datetime])) * 1000, 
                    0), 114)
FROM @temp
GROUP BY basescore
ORDER BY basescore

输出 -

basescore   datediff
----------- ----------
1           01:00:00:0
2           00:03:00:0
于 2013-07-29T07:29:15.267 回答
3

使用您发布的示例数据(并稍作调整),这是我解决问题的尝试:

SELECT BASESCORE, 
       Datediff(dd, MINDATE, MAXDATE) 
FROM   (SELECT BASESCORE, 
               Cast(Max(DATETIME) AS DATETIME) MaxDate, 
               Cast(Min(DATETIME) AS DATETIME) MinDate 
        FROM   table1 
        GROUP  BY BASESCORE)T 

您可以查看SQL Fiddle上的完整工作版本。

让我知道是否有什么我误解了;-)

于 2013-07-29T07:54:30.293 回答
2

早些时候您向您展示了日期格式,您再次删除了格式,但我有时间复制它。在我看来,这是您想要的查询和结果:

declare @t table(datetime char(20), basescore int)
insert @t values('20/Mar/2013:03:17:43', 1),('20/Mar/2013:03:17:43', 2)
                ('20/Mar/2013:04:17:43', 1),('20/Mar/2013:03:20:43', 2)

SELECT basescore,  MAX(convertdate) - MIN(convertdate) AS datediff
-- when selecting from your own table remove '@t' from next line
FROM @t log
cross apply(select cast(stuff([datetime], 12, 1, ' ') as datetime) convertdate) a
group by basescore order by basescore

结果:

basescore   datediff
1   1900-01-01 01:00:00.000
2   1900-01-01 00:03:00.000
于 2013-07-29T07:56:56.263 回答
1

我从您上次编辑的帖子中看到,字段日期时间的值,例如“20/Mar/2013:03:17:44”。datetime 值的格式无法通过简单的方式进行转换。

试试这个查询:

SELECT BASESCORE, 
       [DATEDIFF]= Datediff(second, Min(CONVERT(DATETIME, 
                                        LEFT(Rtrim(Ltrim(DATETIME)), 
                                        11) + ' ' 
                                        + RIGHT(Rtrim(Ltrim(DATETIME) 
                                        ), 8))), Max 
                               ( 
                               CONVERT(DATETIME, LEFT(Rtrim(Ltrim(DATETIME)), 11 
                                                 ) + ' ' 
                                                 + RIGHT(Rtrim(Ltrim(DATETIME)), 
                                                 8)))) 
FROM   temptabel 
GROUP  BY BASESCORE 
ORDER  BY BASESCORE 

输出:

Basescore    datediff
1            1
2            0
3            0

你可以在SQLFiddle看到

于 2013-07-29T08:00:37.300 回答
1

使用DATEDIFF().

句法 :DATEDIFF(datepart,startdate,enddate)

微软

于 2013-07-29T07:24:27.220 回答