获取在30分钟内关闭的订单数量

时间:2017-12-06 07:55:53

标签: sql sql-server

以下查询返回以下输出

输出

OutletID    Avg Receive  Avg Ready  Avg Dispatch   Avg Close       Customers           Orders
32            58           29           0             200              6               6
23            27           1            0              3               7               3

查询

SELECT
  Outlets.OutletID,
  AVG(DATEDIFF(mi, Orders.OrderDate, ReceivedOrders.ReceivingDate)) AS [Avg Receive],
  AVG(DATEDIFF(mi, ReceivedOrders.ReceivingDate, ReadyOrders.ReadyDate)) AS [Avg Ready],
  AVG(DATEDIFF(mi, ReadyOrders.ReadyDate, DispatchedOrders.DispatchedDate)) AS [Avg Dispatch],
  AVG(DATEDIFF(mi, DispatchedOrders.DispatchedDate, ClosedOrders.ClosingDate)) AS [Avg Close],
  Count (dbo.Orders.CustomerID) as Customers,
  Count (dbo.Orders.OrderID)  as Orders
FROM dbo.Orders
INNER JOIN dbo.Outlets
  ON dbo.Orders.OutletID = dbo.Outlets.OutletID
INNER JOIN dbo.Brands
  ON dbo.Brands.BrandID = dbo.Outlets.BrandID
INNER JOIN dbo.ReceivedOrders
  ON dbo.Orders.OrderID = dbo.ReceivedOrders.OrderID
LEFT JOIN dbo.ReadyOrders
  ON dbo.Orders.OrderID = dbo.ReadyOrders.OrderID
LEFT JOIN dbo.DispatchedOrders
  ON dbo.Orders.OrderID = dbo.DispatchedOrders.OrderID
LEFT JOIN dbo.ClosedOrders
  ON dbo.Orders.OrderID = dbo.ClosedOrders.OrderID
WHERE Orders.OrderDate BETWEEN '01/Dec/2017 10:00 AM' AND '05/Dec/2017 12:00 am'
and dbo.Orders.OrderID not in (select OrderID from [dbo].[CanceledOrders])
GROUP BY Outlets.OutletID,
         Brands.BrandName,
         dbo.Outlets.OutletName
ORDER BY dbo.Outlets.OutletName, Brands.BrandName

我正在寻找的是获得每个商店30分钟内关闭的订单数量,因此输出应该如下所示。

所需输出

OutletID    Avg Receive  Avg Ready  Avg Dispatch   Avg Close       Customers           Orders   lessthan30
32            58           29           0             200              6               6           4
23            27           1            0              3               7               3           2

获取结算时间和订单时间之间差异的查询

    SELECT
  COUNT(Orders.OrderID) AS [less30]
FROM [dbo].[Orders]
INNER JOIN dbo.ClosedOrders
  ON dbo.ClosedOrders.[OrderID] = Orders.OrderID
WHERE [OrderStatus] = 'Closed'
AND DATEDIFF(mi, Orders.OrderDate, ClosedOrders.ClosingDate) < 30
AND Orders.OrderDate BETWEEN  '01/Dec/2017 10:00 AM' AND '05/Dec/2017 12:00 am'

那么如何将两个查询组合成一个查询

1 个答案:

答案 0 :(得分:0)

我没有确切的架构,但我认为这应该有效

SELECT
  Outlets.OutletID,
  AVG(DATEDIFF(mi, Orders.OrderDate, ReceivedOrders.ReceivingDate)) AS [Avg Receive],
  AVG(DATEDIFF(mi, ReceivedOrders.ReceivingDate, ReadyOrders.ReadyDate)) AS [Avg Ready],
  AVG(DATEDIFF(mi, ReadyOrders.ReadyDate, DispatchedOrders.DispatchedDate)) AS [Avg Dispatch],
  AVG(DATEDIFF(mi, DispatchedOrders.DispatchedDate, ClosedOrders.ClosingDate)) AS [Avg Close],
  Count (dbo.Orders.CustomerID) as Customers,
  Count (dbo.Orders.OrderID)  as Orders,
  count less30 as Closedin30
FROM dbo.Orders
INNER JOIN dbo.Outlets
  ON dbo.Orders.OutletID = dbo.Outlets.OutletID
INNER JOIN dbo.Brands
  ON dbo.Brands.BrandID = dbo.Outlets.BrandID
INNER JOIN dbo.ReceivedOrders
  ON dbo.Orders.OrderID = dbo.ReceivedOrders.OrderID
LEFT JOIN dbo.ReadyOrders
  ON dbo.Orders.OrderID = dbo.ReadyOrders.OrderID
LEFT JOIN dbo.DispatchedOrders
  ON dbo.Orders.OrderID = dbo.DispatchedOrders.OrderID
LEFT JOIN dbo.ClosedOrders
  ON dbo.Orders.OrderID = dbo.ClosedOrders.OrderID
  left join 
  (
    SELECT
    Orders.OrderID as less30
    FROM [dbo].[Orders]
        INNER JOIN dbo.ClosedOrders
    ON dbo.ClosedOrders.[OrderID] = Orders.OrderID
        WHERE [OrderStatus] = 'Closed'
    AND DATEDIFF(mi, Orders.OrderDate, ClosedOrders.ClosingDate) < 30
    AND Orders.OrderDate BETWEEN  '01/Dec/2017 10:00 AM' AND '05/Dec/2017 12:00 am'
  ) closedIntime
  on
  (
  dbo.Orders.OrderID = closedIntime.less30
  )
WHERE Orders.OrderDate BETWEEN '01/Dec/2017 10:00 AM' AND '05/Dec/2017 12:00 am'
and dbo.Orders.OrderID not in (select OrderID from [dbo].[CanceledOrders])
GROUP BY Outlets.OutletID,
         Brands.BrandName,
         dbo.Outlets.OutletName
ORDER BY dbo.Outlets.OutletName, Brands.BrandName