2017-04-01 162 views
0
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子句中。這個查詢有什麼問題,爲什麼我得到這個錯誤?

+0

是的,你是指向右側,但什麼是錯? –

回答

0

你只是通過e.Amount訂購,而不是在第四集合列SUM(Amount) Amount從最後一排:

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 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 
0

不能UNION ALLTOP之類的語句,你可以把它們放在單獨的Common Table Expressions然後UNION ALL那些。

所以這樣的事情,雖然你可能需要包裝的UNION ALL在另一個CTE能夠做到又一TOP 5

WITH FirstTop5 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 
) 
, SecondTop5 AS 
(
    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 * FROM FirstTop5 
UNION ALL 
SELECT * FROM SecondTop5 

然而,它可以更好地從你的CTE,只是去掉TOP 5限制做一個UNION ALL,然後在你的外部查詢中執行TOP 5