这个查询有什么问题我为什么会收到此错误?

时间:2017-04-01 08:47:38

标签: sql sql-server tsql

 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子句中。

2 个答案:

答案 0 :(得分:0)

您不能UNION ALL这样的TOP语句,您可以将它们分别放在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

但是,最好从TOP 5中移除CTE限制,然后执行UNION ALL,然后在外部查询中执行TOP 5

答案 1 :(得分: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