【发布时间】:2019-02-28 18:44:12
【问题描述】:
我有一个 SQL 语句 (MS SQL Server 2012),它可以简单计算某些记录中的日期差异。我也想返回 DATEDIFF 的总和。
这是我的查询:
with cte as (
select ROW_NUMBER () OVER (ORDER BY Date) as ID, Die_ID, Date, Status
from Tooling_Status
where (date between '2018-02-27 00:00:00' and '2019-02-27 11:59:59')
and Date is not null)
select DATEDIFF (ss, c1.Date, min (c2.Date)) as Seconds, convert (nvarchar (10), c1.Date, 120) as DIA
from cte c1
left outer join cte c2
on c1.Date < c2.Date and c1.Die_ID = c2.Die_ID
where c1.Status = 2 and c2.Status = 1
group by c1.Date
order by DIA desc
这是我的结果:
Seconds DAY
2253 2019-02-27
166 2019-02-27
17 2019-02-27
104 2019-02-27
36 2019-02-27
11 2019-02-07
20 2019-02-07
32 2019-02-07
22 2019-02-07
27 2019-02-07
33 2019-02-07
15 2019-02-07
34 2019-02-07
120 2019-02-04
99420 2019-01-07
60 2018-09-26
我需要这个结果:
Seconds DAY
2732 2019-02-27
194 2019-02-07
120 2019-02-04
99420 2019-01-07
60 2018-09-26
感谢您的帮助!
【问题讨论】:
-
您发布了 html -- 这不是一个允许这样做的网站。你的问题应该是文字
-
虽然 markdown 更好地接受一些基本的 HTML 标签以避免使用它们,因为现在将代码 sn-ps 重新格式化为更具可读性的内容并非易事
-
请在发帖前检查格式选项。无需将所有 HTML 标记放在那里。这只会弄乱语法突出显示。我已经修复了这个帖子,但你下次应该自己做;写得好、格式好的问题更有可能得到回答。
标签: sql-server sum datediff