将'总计'作为最终列的SQL语句

时间:2015-11-24 23:39:08

标签: mysql sql-server

我正在尝试运行SQL查询,第一列是'month'。每列将有一个数字值(或月份数),最后一列将显示“总计”。问题在于,当我尝试让它们进行排序时,每次到达Totals列时都会给出错误,因为它正在查找INT值。所以,例如,我有这个查询:

SELECT CY.Month, CY.PaymentAmount_ThisYear, LY.PaymentAmount_LastYear FROM
(SELECT DATEPART(MM, Orders.OrderDate) AS Month, SUM(Orders.PaymentAmount) as PaymentAmount_ThisYear 
    FROM Orders WHERE DATEPART(YY, Orders.OrderDate) = DATEPART(YY, DATEADD(MM, -1, CURRENT_TIMESTAMP))
    GROUP BY DATEPART(MM, Orders.OrderDate)
) CY
INNER JOIN 
(SELECT DATEPART(MM, Orders.OrderDate) AS Month, SUM(Orders.PaymentAmount) as PaymentAmount_LastYear
    FROM Orders WHERE DATEPART(YY, Orders.OrderDate) = DATEPART(YY, DATEADD(MM, -13, CURRENT_TIMESTAMP))
    GROUP BY DATEPART(MM, Orders.OrderDate)
) LY
ON CY.Month = LY.Month
UNION
SELECT 'Totals' AS Month, CY.PaymentAmount_ThisYear, LY.PaymentAmount_LastYear FROM
(SELECT SUM(Orders.PaymentAmount) AS PaymentAmount_ThisYear
    FROM Orders WHERE DATEPART(YY, Orders.OrderDate) = DATEPART(YY, DATEADD(MM, -1, CURRENT_TIMESTAMP))
) CY,
(SELECT SUM(Orders.PaymentAmount) AS PaymentAmount_LastYear
    FROM Orders WHERE DATEPART(YY, Orders.OrderDate) = DATEPART(YY, DATEADD(MM, -13, CURRENT_TIMESTAMP))
) LY

如果我将'Totals'更改为13,它会起作用并将总数放在最底部,但该行显示为'13'而不是'Totals'。

enter image description here

所以我接着尝试的是在查询的最开始我改变了这个:

SELECT CY.Month

SELECT CAST(CY.Month AS VARCHAR(10)) AS Month

但是,当我这样做时,它命令他们错了:

1
10
11
5

因为它将它们识别为varchar值。我基本上是在寻找这个以数字方式订购月份,但最后一栏却说'总计'。

我也尝试过做

ORDER BY CASE ISNUMERIC(月) 1那么那么(月份为INT) ELSE月 END

但这给了我一个'不明确的专栏名'月'。'错误。

我有点卡住,有什么想法吗?谢谢!

2 个答案:

答案 0 :(得分:1)

我认为您可以大大简化查询并使用GROUP BY的ROLLUP选项来完成总计,例如:

SELECT 
      CASE 
        WHEN GROUPING(MONTH(orders.orderdate)) = 1 
        THEN 'Totals' 
        ELSE CONVERT(CHAR(2), MONTH(orders.orderdate)) 
      END AS [Month]
    , SUM(
        CASE 
            WHEN YEAR(orders.orderdate) = Datepart(yy, Dateadd(mm, -1, CURRENT_TIMESTAMP)) 
            THEN orders.paymentamount 
            ELSE 0.00
        END
        )   AS PaymentAmount_ThisYear 
    , SUM(
        CASE 
            WHEN YEAR(orders.orderdate) = Datepart(yy, Dateadd(mm, -13, CURRENT_TIMESTAMP)) 
            THEN orders.paymentamount 
            ELSE 0.00 
        END
        )   AS PaymentAmount_LastYear 
FROM   
    orders 
GROUP BY   
    MONTH(orders.orderdate)
WITH ROLLUP

答案 1 :(得分:0)

您可以尝试将当前查询放在内联视图中,然后再排序

SELECT Month, PaymentAmount_ThisYear, PaymentAmount_LastYear
FROM (
    SELECT CAST(CY.Month AS VARCHAR(10)) AS Month, CY.PaymentAmount_ThisYear, LY.PaymentAmount_LastYear FROM
        (SELECT DATEPART(MM, Orders.OrderDate) AS Month, SUM(Orders.PaymentAmount) as PaymentAmount_ThisYear 
            FROM Orders WHERE DATEPART(YY, Orders.OrderDate) = DATEPART(YY, DATEADD(MM, -1, CURRENT_TIMESTAMP))
            GROUP BY DATEPART(MM, Orders.OrderDate)
        ) CY
    INNER JOIN 
        (SELECT DATEPART(MM, Orders.OrderDate) AS Month, SUM(Orders.PaymentAmount) as PaymentAmount_LastYear
            FROM Orders WHERE DATEPART(YY, Orders.OrderDate) = DATEPART(YY, DATEADD(MM, -13, CURRENT_TIMESTAMP))
            GROUP BY DATEPART(MM, Orders.OrderDate)
        ) LY
    ON CY.Month = LY.Month
    UNION
    SELECT 'Totals' AS Month, CY.PaymentAmount_ThisYear, LY.PaymentAmount_LastYear FROM
        (SELECT SUM(Orders.PaymentAmount) AS PaymentAmount_ThisYear
            FROM Orders WHERE DATEPART(YY, Orders.OrderDate) = DATEPART(YY, DATEADD(MM, -1, CURRENT_TIMESTAMP))
        ) CY,
        (SELECT SUM(Orders.PaymentAmount) AS PaymentAmount_LastYear
            FROM Orders WHERE DATEPART(YY, Orders.OrderDate) = DATEPART(YY, DATEADD(MM, -13, CURRENT_TIMESTAMP))
        ) LY) T
ORDER BY CASE WHEN ISNUMERIC(Month) = 1 THEN 0 ELSE 1 END, Month