某些列的SQL行总和

时间:2012-06-22 17:23:44

标签: sql sql-server tsql sum

我正在尝试开发一个SQL字段,它是除前两行之外的所有列的总和。下面是我的代码,它返回MonthYear,Total,Sum1,Sum2和Sum3列。我想要一个总和为Sum1,Sum2和Sum3的FinalSum。这用于反击检查总列的准确性。有没有办法做到这一点?运行Microsoft SQL Server 2005 - T-SQL

SELECT MonthYear,
            COUNT(*) AS Total,
            ISNULL(
                SUM(
                    CASE 
                    WHEN Status='Sum1'                  THEN 1
                    ELSE 0
                    END
                )
            ,0) AS [Sum1],
ISNULL(
                SUM(
                    CASE 
                    WHEN Status='Sum2'                  THEN 1
                    ELSE 0
                    END
                )
            ,0) AS [Sum2],
ISNULL(
                SUM(
                    CASE 
                    WHEN Status='Sum3'                  THEN 1
                    ELSE 0
                    END
                )
            ,0) AS [Sum3]
FROM tablename

GROUP BY
    MonthYear

5 个答案:

答案 0 :(得分:6)

您的查询缺少FROM。无论如何,一种方法是使用派生表:

SELECT *, [Sum1]+[Sum2]+[Sum3] AS TotalSum
FROM (Your Current Select Here) AS T

对于SQL Server 2005+,您还可以使用CTE:

;WITH CTE AS 
(
    Your Current Select Here
)
SELECT *, [Sum1]+[Sum2]+[Sum3] AS TotalSum
FROM CTE

答案 1 :(得分:1)

好的 - 可能不是最优雅的方式(复制保存在这里on SqlFiddle

CREATE TABLE this_name 
(
    monthYear DateTime,
    status varchar(20), 
   );

INSERT INTO this_name
(monthYear, status)
VALUES
('01 jan 2012', 'Sum1'),
('01 jan 2012', 'Sum1'),
('01 feb 2012', 'Sum2'),
('01 feb 2012', 'Sum1'),
('01 apr 2012', 'Sum3'),
('01 jan 2013', 'Sum3'),
('01 jan 2013', 'Sum3'),
('01 jan 2012', 'Sum3')

;WITH myCTE (MonthYear,Total, Sum1, Sum2, Sum3)
AS
(
SELECT MonthYear,
       COUNT(*) AS Total,
       SUM(CASE WHEN ISNULL(Status,'X')='Sum1' THEN 1 ELSE 0 END) AS [Sum1],
       SUM(CASE WHEN ISNULL(Status,'X')='Sum2'THEN 1 ELSE 0 END ) AS [Sum2],
       SUM(CASE WHEN ISNULL(Status,'X')='Sum3' THEN 1 ELSE 0 END) AS [Sum3] 
FROM this_name
GROUP BY MonthYear
)   
SELECT 
  MonthYear,
  Total, 
  Sum1, 
  Sum2, 
  Sum3,
  (Sum1 + Sum2 + Sum3) [CountTotal]
FROM myCTE

另一种选择是[SQLFiddle];

SELECT MonthYear,
   COUNT(*) AS Total,
   SUM(CASE WHEN ISNULL(Status,'X')='Sum1' THEN 1 ELSE 0 END) AS [Sum1],
   SUM(CASE WHEN ISNULL(Status,'X')='Sum2'THEN 1 ELSE 0 END ) AS [Sum2],
   SUM(CASE WHEN ISNULL(Status,'X')='Sum3' THEN 1 ELSE 0 END) AS [Sum3], 

   SUM(CASE WHEN ISNULL(Status,'X')IN('Sum1','Sum2','Sum3') THEN 1 ELSE 0 END) AS [SumExtra]  
FROM this_name
GROUP BY MonthYear

答案 2 :(得分:0)

declare @sum1 int, @sum2 int, @sum3 int
declare @totalSum int = 0

select @sum1 = ISNULL(SUM(CASE WHEN Status='Sum1' THEN 1 ELSE 0 END), 0)
from tablename

select @sum2 = ISNULL(SUM(CASE WHEN Status='Sum2' THEN 1 ELSE 0 END), 0)
from tablename

select @sum3 = ISNULL(SUM(CASE WHEN Status='Sum3' THEN 1 ELSE 0 END), 0)
from tablename

set @totalSum = @sum1 + @sum2 + @sum3

SELECT MonthYear, COUNT(*) AS Total,
            ISNULL(SUM(
                    CASE 
                    WHEN Status='Sum1' THEN 1
                    ELSE 0
                    END
                ), 0) AS [Sum1],
            ISNULL(SUM(
                    CASE 
                    WHEN Status='Sum2' THEN 1
                    ELSE 0
                    END), 0) AS [Sum2],
            ISNULL(SUM(
                    CASE 
                    WHEN Status='Sum3' THEN 1
                    ELSE 0
                    END), 0) AS [Sum3],
            @totalSum
FROM tablename

GROUP BY
    MonthYear

答案 3 :(得分:0)

使用Temp表的一种方法

CREATE TABLE #Table2
(
    MonthYear VARCHAR(6) NOT NULL PRIMARY KEY,
    Total INT NOT NULL,
    Sum1 INT NOT NULL,
    Sum2 INT NOT NULL,
    Sum3 INT NOT NULL
) ;

INSERT INTO #Table2
SELECT
        MonthYear,
        COUNT(*) AS Total,
        SUM(CASE WHEN ISNULL(Status, '') = 'Sum1' THEN 1 ELSE 0 END),
        SUM(CASE WHEN ISNULL(Status, '') = 'Sum2' THEN 1 ELSE 0 END),
        SUM(CASE WHEN ISNULL(Status, '') = 'Sum3' THEN 1 ELSE 0 END)
FROM tablename
GROUP BY MonthYear ;

SELECT *, Sum1Count + Sum2Count + Sum3Count AS FinalSum FROM #Table2 ;

答案 4 :(得分:0)

重复计算的另一种方法

SELECT
        MonthYear,
        COUNT(*) AS Total,
        SUM(CASE WHEN ISNULL(Status, '') = 'Sum1' THEN 1 ELSE 0 END) AS [Sum1],
        SUM(CASE WHEN ISNULL(Status, '') = 'Sum2' THEN 1 ELSE 0 END) AS [Sum2],
        SUM(CASE WHEN ISNULL(Status, '') = 'Sum3' THEN 1 ELSE 0 END) AS [Sum3],
        SUM(CASE WHEN ISNULL(Status, '') = 'Sum1' THEN 1 ELSE 0 END) +
        SUM(CASE WHEN ISNULL(Status, '') = 'Sum2' THEN 1 ELSE 0 END) +
        SUM(CASE WHEN ISNULL(Status, '') = 'Sum3' THEN 1 ELSE 0 END) AS [FinalSum]
FROM tablename
GROUP BY MonthYear ;