简体   繁体   English

T-SQL-两个日期之间的总差

[英]T-SQL - Total difference between two dates

I have two tables, Orders & Order History , the Orders table has the Order Number and the Order Date which is the date that the order was actually placed. 我有两个表, 订单订单历史记录 ,订单表具有订单号订单日期 ,即订单实际下达的日期。

This is demonstrated in the below schema and demonstration data; 下面的模式和演示数据对此进行了演示;

CREATE TABLE #Orders
(
OrderNumber INT,
OrderDate DATETIME
)

INSERT INTO #Orders (OrderNumber,OrderDate)
VALUES
(001,'2019-04-16 07:08:08.567'),
(002,'2019-03-22 07:08:08.567'),
(003,'2019-06-30 07:08:08.567'),
(004,'2019-01-05 07:08:08.567'),
(005,'2019-02-19 07:08:08.567')

The Order audit table also contains the Order Number and the Event Date which is the date that the order status changed. 订单审核表还包含订单编号事件日期 ,即事件状态更改的日期。

This is demonstrated in the below schema and demonstration data. 在以下架构和演示数据中对此进行了演示。

CREATE TABLE #Order_Audit
(
OrderNumber INT,
EventDate DATETIME,
Status INT
)

INSERT INTO #Order_Audit (OrderNumber,EventDate,Status)
VALUES
(001,'2019-04-16 07:08:08.567',1),
(001,'2019-04-19 07:08:08.567',2),
(001,'2019-04-22 07:08:08.567',3),
(001,'2019-04-28 07:08:08.567',4),
(001,'2019-04-30 07:08:08.567',5),
(002,'2019-03-22 07:08:08.567',1),
(002,'2019-03-24 07:08:08.567',2),
(002,'2019-03-26 07:08:08.567',3),
(002,'2019-04-01 07:08:08.567',4),
(002,'2019-04-10 07:08:08.567',5),
(003,'2019-06-30 07:08:08.567',1),
(003,'2019-07-15 07:08:08.567',2),
(003,'2019-07-19 07:08:08.567',3),
(003,'2019-07-20 07:08:08.567',4),
(003,'2019-07-21 07:08:08.567',5),
(004,'2019-01-05 07:08:08.567',1),
(004,'2019-01-06 07:08:08.567',2),
(004,'2019-01-07 07:08:08.567',3),
(004,'2019-01-08 07:08:08.567',4),
(004,'2019-01-09 07:08:08.567',5),
(005,'2019-02-19 07:08:08.567',1),
(005,'2019-03-19 07:08:08.567',2),
(005,'2019-03-21 07:08:08.567',3),
(005,'2019-03-22 07:08:08.567',4),
(005,'2019-03-23 07:08:08.567',5)

Below is the query that I have at the moment, it will give me the difference between the Event Date and the Order Date that the order was placed. 以下是我目前的查询,它将为我提供事件日期和下订单的日期之间的区别。

The query has been simplified however the key columns are included. 该查询已简化,但是包含了关键列。 This is being executed on SQL Server 2012 SP4 这是在SQL Server 2012 SP4上执行的

SELECT  
    O.OrderNumber,
    DATEDIFF(DAY,O.OrderDate,OA.EventDate) AS [Day-Diff]
FROM #Orders O

INNER JOIN #Order_Audit OA ON OA.OrderNumber = O.OrderNumber

The above query outputs like this 上面的查询输出是这样的

|---------------------|------------------|
|      OrderNumber    |     DayDiff      |
|---------------------|------------------|
|          001        |         0        |
|---------------------|------------------|
|          001        |         3        |
|---------------------|------------------|
|          001        |         6        |
|---------------------|------------------|
|          001        |         12       |
|---------------------|------------------|
|          001        |         14       |
|---------------------|------------------|
|          002        |         0        |
|---------------------|------------------|
|          002        |         2        |
|---------------------|------------------|
|          002        |         4        |
|---------------------|------------------|
|          002        |         10       |
|---------------------|------------------|
|          002        |         19       |
|---------------------|------------------|

What I actually need is a query that will output more like this 我真正需要的是一个查询,它将输出更多这样的内容

|---------------------|------------------|
|      OrderNumber    |     DayDiff      |
|---------------------|------------------|
|          001        |                  |
|---------------------|------------------|
|          001        |                  |
|---------------------|------------------|
|          001        |                  |
|---------------------|------------------|
|          001        |                  |
|---------------------|------------------|
|          001        |                  |
|---------------------|------------------|
|          Total      |        14        |
|---------------------|------------------|
|          002        |                  |
|---------------------|------------------|
|          002        |                  |
|---------------------|------------------|
|          002        |                  |
|---------------------|------------------|
|          002        |                  |
|---------------------|------------------|
|          002        |                  |
|---------------------|------------------|
|          Total      |        19        |
|---------------------|------------------|

However I can't figure out how to get the difference between the Order Date and the most recent Event Date for each order and add it below that group of order events (as shown above) - I am not even sure it is possible in T-SQL and should possibly be handled at the application level. 但是我无法弄清楚如何获取每个订单的订单日期和最近的事件日期之间的差异,并将其添加到该订单事件组的下方(如上所示)-我什至不确定T是否有可能-SQL,应该在应用程序级别进行处理。

You can try this below. 您可以在下面尝试。 I have created the Total label as OrderNumber + Total for ordering. 我已经将Total标签创建为OrderNumber + Total以进行订购。

SELECT  
   CAST(O.OrderNumber AS VARCHAR) +  ' Total' OrderNumber,
    MAX(DATEDIFF(DAY,O.OrderDate,OA.EventDate)) AS [Day-Diff]
FROM #Orders O

INNER JOIN #Order_Audit OA ON OA.OrderNumber = O.OrderNumber
GROUP BY CAST(O.OrderNumber AS VARCHAR) +  ' Total'

UNION ALL

SELECT  
   CAST(O.OrderNumber AS VARCHAR) OrderNumber,
   NULL AS [Day-Diff]
FROM #Orders O

INNER JOIN #Order_Audit OA ON OA.OrderNumber = O.OrderNumber

ORDER BY 1

For the totals you can group by ordernumber to get the last eventdate and then find the difference from the corresponding orderdate . 对于总计您可以group by ordernumber得到最后eventdate然后找到相应的差异orderdate
Then use UNION ALL: 然后使用UNION ALL:

select t.OrderNumber, t.DayDiff
from (
  select ordernumber nr, cast(ordernumber as varchar(10)) OrderNumber, null DayDiff, 0 col 
  from order_audit
  union all
  select a.ordernumber nr, 'Total', datediff(day, o.orderdate, a.eventdate) DayDiff, 1 col
  from orders o inner join (
    select 
    ordernumber, max(eventdate) eventdate
    from order_audit
    group by ordernumber
  ) a on a.ordernumber = o.ordernumber
) t
order by t.nr, t.col

See the demo . 参见演示
Results: 结果:

> OrderNumber | DayDiff
> :---------- | ------:
> 1           |    
> 1           |    
> 1           |    
> 1           |    
> 1           |    
> Total       |      14
> 2           |    
> 2           |    
> 2           |    
> 2           |    
> 2           |    
> Total       |      19
> 3           |    
> 3           |    
> 3           |    
> 3           |    
> 3           |    
> Total       |      21
> 4           |    
> 4           |    
> 4           |    
> 4           |    
> 4           |    
> Total       |       4
> 5           |    
> 5           |    
> 5           |    
> 5           |    
> 5           |    
> Total       |      32

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM