SQL按别名的总和排序

时间:2017-03-19 07:16:14

标签: sql sql-server sum alias

前言,这是Microsoft SQL Server。我有不同年份的不同收入的几个不同栏目。我需要按每个给定产品的总销售额进行排序,但不要将其作为实际列报告。注释行是我尝试这样做的方式,但似乎你不能通过别名来命令。怎么会这样呢?

title

4 个答案:

答案 0 :(得分:2)

两种方法

  1. 将您的查询作为内部查询和订单
  2. 按顺序排列完整的公式
  3. 查询#1

    select * from 
        (
        SELECT 
            StockItems.StockItemID, 
            StockItemName,
            SUM(CASE WHEN OrderDate >= '2013-01-01'
                AND OrderDate < '2014-01-01'
                THEN Quantity * OrderLines.UnitPrice END) AS Sales2013,
            SUM(CASE WHEN OrderDate >= '2014-01-01'
                AND OrderDate < '2015-01-01'
                THEN Quantity * OrderLines.UnitPrice END) AS Sales2014,
            SUM(CASE WHEN OrderDate >= '2014-01-01'
                AND OrderDate < '2015-01-01'
                THEN Quantity * OrderLines.UnitPrice END) AS Sales2015,
            SUM(CASE WHEN OrderDate >= '2015-01-01'
                AND OrderDate < '2016-01-01'
                THEN Quantity * OrderLines.UnitPrice END) AS Sales2016
        FROM 
            Warehouse.StockItems
    
            INNER JOIN Sales.OrderLines
                ON Warehouse.StockItems.StockItemID = Sales.OrderLines.StockItemID
            INNER JOIN Sales.Orders
                ON Sales.OrderLines.OrderID = Sales.Orders.OrderID
    
        GROUP BY 
            StockItems.StockItemID, 
            StockItemName
        ) temp
    order by SUM(Sales2013 + Sales2014 + Sales2015 + Sales2016) 
    

    查询#2

     SELECT 
            StockItems.StockItemID, 
            StockItemName,
            SUM(CASE WHEN OrderDate >= '2013-01-01'
                AND OrderDate < '2014-01-01'
                THEN Quantity * OrderLines.UnitPrice END) AS Sales2013,
            SUM(CASE WHEN OrderDate >= '2014-01-01'
                AND OrderDate < '2015-01-01'
                THEN Quantity * OrderLines.UnitPrice END) AS Sales2014,
            SUM(CASE WHEN OrderDate >= '2014-01-01'
                AND OrderDate < '2015-01-01'
                THEN Quantity * OrderLines.UnitPrice END) AS Sales2015,
            SUM(CASE WHEN OrderDate >= '2015-01-01'
                AND OrderDate < '2016-01-01'
                THEN Quantity * OrderLines.UnitPrice END) AS Sales2016
        FROM 
            Warehouse.StockItems
    
            INNER JOIN Sales.OrderLines
                ON Warehouse.StockItems.StockItemID = Sales.OrderLines.StockItemID
            INNER JOIN Sales.Orders
                ON Sales.OrderLines.OrderID = Sales.Orders.OrderID
    
        GROUP BY 
            StockItems.StockItemID, 
            StockItemName
    
    order by SUM(Quantity * OrderLines.UnitPrice)
    

答案 1 :(得分:1)

SELECT StockItemID
    , StockItemName
    , Sales2013
    , Sales2014
    , Sales2015
    , Sales2016
    , Sales2013 + Sales2014 + Sales2015 + Sales2016 total_sales
FROM 
(
    SELECT 
        StockItems.StockItemID, 
        StockItemName,
        SUM(CASE WHEN OrderDate >= '2013-01-01'
        AND OrderDate < '2014-01-01'
        THEN Quantity * OrderLines.UnitPrice END) AS Sales2013,
        SUM(CASE WHEN OrderDate >= '2014-01-01'
        AND OrderDate < '2015-01-01'
        THEN Quantity * OrderLines.UnitPrice END) AS Sales2014,
        SUM(CASE WHEN OrderDate >= '2014-01-01'
        AND OrderDate < '2015-01-01'
        THEN Quantity * OrderLines.UnitPrice END) AS Sales2015,
        SUM(CASE WHEN OrderDate >= '2015-01-01'
        AND OrderDate < '2016-01-01'
        THEN Quantity * OrderLines.UnitPrice END) AS Sales2016
    FROM 
        Warehouse.StockItems

        INNER JOIN Sales.OrderLines
        ON Warehouse.StockItems.StockItemID = Sales.OrderLines.StockItemID
        INNER JOIN Sales.Orders
        ON Sales.OrderLines.OrderID = Sales.Orders.OrderID

    GROUP BY 
        StockItems.StockItemID, 
        StockItemName
)
ORDER BY total_sales

答案 2 :(得分:1)

添加SUM列:

SUM(CASE WHEN OrderDate >= '2013-01-01'
        AND OrderDate < '2016-01-01'
        THEN Quantity * OrderLines.UnitPrice END) AS Sales_SUM

然后按Sales_SUM订购

答案 3 :(得分:0)

嗯,性能最佳的解决方案需要使用PIVOT,我将其包装在CTE中以提高可读性:

;WITH pvt AS (
    SELECT 
        StockItems.StockItemID, 
        StockItemName,
        YEAR(OrderDate) AS OrderYear,
        Quantity * OrderLines AS SalesTotal,
        SUM(Quantity * OrderLines) OVER (PARTITION BY StockItems.StockItemID) AS SalesForStockItemForYear 
    FROM 
        Warehouse.StockItems
        INNER JOIN Sales.OrderLines
            ON Warehouse.StockItems.StockItemID = Sales.OrderLines.StockItemID
        INNER JOIN Sales.Orders
            ON Sales.OrderLines.OrderID = Sales.Orders.OrderID
    WHERE
        YEAR(OrderDate) IN (2013,2014,2015,2016)
    PIVOT (
        SUM(SalesTotal) FOR OrderYear IN ([2013],[2014],[2015],[2016])
    )
)

SELECT DISTINCT
    pvt.StockItemID,
    pvt.StockItemName,
    pvt.[2013] as Sales2013,
    pvt.[2014] as Sales2014,
    pvt.[2015] as Sales2015,
    pvt.[2016] as Sales2016
FROM
    pvt
ORDER BY
    pvt.SalesForStockItemForYear DESC