获取每个品牌 sql 的小时销售额



我已经拖曳品牌brand1brand2我正在寻找每个品牌的小时销售额。

我尝试使用以下查询执行此操作,但它返回以下输出:

查询

WITH MyGroupCte
AS (SELECT
  OrderDate,
  Brands.BrandID,
  Brands.BrandName,
  SUM((OrderDetails.Quantity * OrderDetails.UnitPrice)) + Orders.DeliveryCharge - ((SUM((OrderDetails.Quantity * OrderDetails.UnitPrice))) * Orders.Discount / 100) AS Amount,
  COUNT(Orders.OrderID) AS Orders
FROM dbo.Orders
INNER JOIN dbo.Outlets
  ON dbo.Orders.OutletID = dbo.Outlets.OutletID
INNER JOIN dbo.OrderDetails
  ON dbo.OrderDetails.OrderID = dbo.Orders.OrderID
INNER JOIN dbo.Brands
  ON dbo.Brands.BrandID = dbo.Outlets.BrandID
WHERE dbo.Orders.OrderStatus = 'Closed'
AND dbo.Orders.OrderDate BETWEEN '06/May/2017 14:00:00.000' AND '06/May/2017 16:00:00.000'
GROUP BY Orders.OrderID,
         dbo.Orders.DeliveryCharge,
         Orders.Discount,
         Brands.BrandID,
         Brands.BrandName,
         Orders.OrderDate)
SELECT
  BrandName AS Brand,
  OrderDate as [Date-Time],
  COUNT(Orders) AS Orders,
  SUM(Amount) AS Amount
FROM MyGroupCte
GROUP BY OrderDate,
         BrandName
ORDER BY Amount DESC

输出

Brand   Date-Time               Orders   Amount
brand1  2017-05-06 14:04:52.000   1      530.00
brand1  2017-05-06 14:05:06.000   1      448.00
brand2  2017-05-06 15:27:21.000   1      347.00
brand2  2017-05-06 15:52:25.000   1      254.00
brand2  2017-05-06 16:06:25.000   1      100.00

取而代之的是,我正在寻找以下格式的输出

Brand   Date-Time               Orders   Amount
brand1  2017-05-06 14:00:00.000   2      978.00
brand2  2017-05-06 15:00:00.000   2      601.00
brand2  2017-05-06 16:00:00.000   2      100.00

使用 DATEADD(hour, DATEDIFF(hour, 0, OrderDate), 0) 将日期时间截断为小时并将其用于分组。

SELECT 
  BrandName AS Brand,
  DATEADD(hour, DATEDIFF(hour, 0, OrderDate), 0) as [Date-Time],
  COUNT(Orders) AS Orders,
  SUM(Amount) AS Amount
FROM MyGroupCte
GROUP BY BrandName, DATEADD(hour, DATEDIFF(hour, 0, OrderDate), 0)

相关内容

  • 没有找到相关文章

最新更新