2
我有兩個品牌brand1
和brand2
我在尋找每個品牌的小時銷量。獲取每個品牌的小時銷售sql
我曾嘗試使用下面的查詢來做到這一點,但它返回下面的輸出:
查詢
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
我得到了這個錯誤「Msg 174,Level 15,State 1,Line 25 datediff函數需要3個參數。 – ayman
@ayman ..它錯過了第三個參數..現在糾正。 –