with cte as(
Select Top 5 MenuItemName AS Name, Sum(Quantity) AS Quantity ,
Sum(Price) AS Amount
FROM [sTMS 3].dbo.Orders
INNER JOIN [sTMS 3].dbo.MenuItems ON [sTMS 3].dbo.Orders.MenuItemId = [sTMS 3].dbo.MenuItems.Id
INNER JOIN [sTMS 3].dbo.Tickets TCK ON TCK.Id = [sTMS 3].dbo.Orders.TicketId
INNER JOIN [sTMS 3].dbo.TicketEntities TEN ON TEN.Ticket_Id = TCK.Id
WHERE
(MenuItems.GroupCode = 'Rides' OR MenuItems.GroupCode = 'Ticket Rides')
AND Orders.CreatedDateTime BETWEEN Convert(DATETIME,'Jan 1 2017 12:00AM',101) and Convert(DATETIME,'Mar 3 2017 12:00AM',101)
AND TEN.EntityCustomData NOT LIKE '%{"Name":"Card Type","Value":"Master Card"}%'
AND TEN.EntityCustomData NOT LIKE '%{"Name":"Card Type","Value":"Technical Card"}%'
AND TEN.EntityCustomData NOT LIKE '%{"Name":"Card Type","Value":"Guest Card"}%'
Group by MenuItemName
Order by Amount Desc UNION ALL
Select Top 5 MenuItemName AS Name, Sum(Quantity) AS Quantity ,
Sum(Price) AS Amount
FROM sTMS10.dbo.Orders
INNER JOIN sTMS10.dbo.MenuItems ON sTMS10.dbo.Orders.MenuItemId = sTMS10.dbo.MenuItems.Id
INNER JOIN sTMS10.dbo.Tickets TCK ON TCK.Id = sTMS10.dbo.Orders.TicketId
INNER JOIN sTMS10.dbo.TicketEntities TEN ON TEN.Ticket_Id = TCK.Id
WHERE
(MenuItems.GroupCode = 'Rides' OR MenuItems.GroupCode = 'Ticket Rides')
AND Orders.CreatedDateTime BETWEEN Convert(DATETIME,'Jan 1 2017 12:00AM',101) and Convert(DATETIME,'Mar 3 2017 12:00AM',101)
AND TEN.EntityCustomData NOT LIKE '%{"Name":"Card Type","Value":"Master Card"}%'
AND TEN.EntityCustomData NOT LIKE '%{"Name":"Card Type","Value":"Technical Card"}%'
AND TEN.EntityCustomData NOT LIKE '%{"Name":"Card Type","Value":"Guest Card"}%'
Group by MenuItemName
Order by Amount Desc)
Select top 5 Name,Sum(Quantity) AS Quantity,Sum(Amount)/``
(Select SUM(Amount) FROM (Select top 5 Sum(e.Amount) Amount From cte e Group by e.Name order by e.Amount desc)a) * 100 AS Percentage,Sum(Amount) AS Amount
,CONVERT(DATETIME,'Jan 1 2017 12:00AM',101) FromDate
,CONVERT(DATETIME,'Mar 3 2017 12:00AM',101) ToDate
From cte Group by Name order by Amount desc
列「cte.Amount」在ORDER BY子句中無效,因爲它不包含在聚合函數或GROUP BY子句中。這個查詢有什麼問題,爲什麼我得到這個錯誤?
是的,你是指向右側,但什麼是錯? –