SQL服务器从子查询中多次计数的总和

时间:2015-10-26 15:31:56

标签: sql-server count sum subquery

我试图在SQl Server中将多个计数的单独总和作为单行。以下是我的查询无效的示例

SELECT 
    SUM(A), SUM(B), SUM(C)
FROM
(
    SELECT
       COUNT(CASE T.ID WHEN 1 THEN T.ID END, '', 0) AS [A]
       COUNT(CASE T.ID WHEN 2 THEN T.ID END, '', 0) AS [B]
       COUNT(CASE T.ID WHEN 3 THEN T.ID END, '', 0) AS [C]
    FROM
        MyTable AS T
        INNER JOIN MyOtherTable AS MOT ON T.IDNum = MOT.IDNum
    WHERE
        YEAR(T.MyDate) = 2015
)

这会抛出无效的语法,因为我需要为子查询添加别名。关于如何使其发挥作用的任何想法?

0 个答案:

没有答案