获取每个SQL行的总和

时间:2017-06-22 09:31:41

标签: sql-server

您好我已经写了下面的查询,我遇到的问题显然是返回整个表的总和(所有员工在一起),我希望它返回每个员工的总金额,这是否可能?

DECLARE @temp TABLE
(
TotalAmount DECIMAL,
ExpenseCategory UNIQUEIDENTIFIER,
EmployeeId UNIQUEIDENTIFIER,
CategoryName NVARCHAR(100)
)

INSERT INTO @temp

SELECT  SUM(dbo.ExpenseDetails.TotalAmount) AS TotalAmount,         
dbo.ExpenseDetails.ExpenseCategory, dbo.ExpenseDetails.EmployeeId, 
dbo.ExpensesCategories.CategoryName
FROM            dbo.ExpenseDetails INNER JOIN
                     dbo.ExpensesCategories ON 
dbo.ExpenseDetails.ExpenseCategory = dbo.ExpensesCategories.CategoryId
WHERE        (CONVERT(DATE, dbo.ExpenseDetails.DateAdded) < CONVERT(DATE, 
DATEADD(m, DATEDIFF(m, 0, CONVERT(DATE, GETDATE())), 23 - 1))) AND 
(CONVERT(DATE, dbo.ExpenseDetails.DateAdded) > CONVERT(DATE,
                      DATEADD(MONTH, - 1, DATEADD(m, DATEDIFF(m, 0, 
CONVERT(DATE, GETDATE())), 23 - 1))))
GROUP BY dbo.ExpenseDetails.ExpenseCategory, dbo.ExpenseDetails.EmployeeId, 
dbo.ExpensesCategories.CategoryName

SELECT
EmployeeId,
(SELECT SUM(TotalAmount) FROM @temp WHERE EmployeeId = [@temp].EmployeeId 
and ExpenseCategory = '6CC0F801-D295-4997-9D1F-3E5B2BE692ED') AS TotalFuel,
(SELECT SUM(TotalAmount) FROM @temp WHERE EmployeeId = [@temp].EmployeeId 
and ExpenseCategory = '3E54C10B-ECBE-4B28-87A4-5A4E43AACDFC') AS TotalRail,
(SELECT SUM(TotalAmount) FROM @temp WHERE EmployeeId = [@temp].EmployeeId 
and ExpenseCategory = '31CC43A4-80E6-4DD4-A28C-039F05AF62A3') AS 
TotalParking,
(SELECT SUM(TotalAmount) FROM @temp WHERE EmployeeId = [@temp].EmployeeId 
and ExpenseCategory = '01250A30-3865-40D2-9E49-DA2B9898B1F4') AS 
TotalMileage,
(SELECT SUM(TotalAmount) FROM @temp WHERE EmployeeId = [@temp].EmployeeId 
and ExpenseCategory = 'F4E4582D-DEDF-499D-BBC1-45003B865218') AS TotalEquip,
(SELECT SUM(TotalAmount) FROM @temp WHERE EmployeeId = [@temp].EmployeeId 
and ExpenseCategory = '55F471C1-6E52-4898-8263-0B7684B78E7C') AS TotalPPS,
(SELECT SUM(TotalAmount) FROM @temp WHERE EmployeeId = [@temp].EmployeeId 
and ExpenseCategory = '1EE0FC40-193F-4720-9143-0B61FFDE7B8D') AS TotalOther

FROM @temp GROUP BY EmployeeId

2 个答案:

答案 0 :(得分:1)

在条件中使用SUM

SELECT
    EmployeeId,
    TotalFuel = SUM(CASE WHEN ExpenseCategory = '6CC0F801-D295-4997-9D1F-3E5B2BE692ED' THEN TotalAmount END)
FROM @temp
GROUP BY EmployeeId

答案 1 :(得分:0)

 SELECT
EmployeeId,
(SELECT SUM(TotalAmount) FROM @temp WHERE ExpenseCategory = '6CC0F801-D295-4997-9D1F-3E5B2BE692ED') AS TotalFuel,
(SELECT SUM(TotalAmount) FROM @temp WHERE ExpenseCategory = '3E54C10B-ECBE-4B28-87A4-5A4E43AACDFC') AS TotalRail,
(SELECT SUM(TotalAmount) FROM @temp WHERE ExpenseCategory = '31CC43A4-80E6-4DD4-A28C-039F05AF62A3') AS TotalParking,
(SELECT SUM(TotalAmount) FROM @temp WHERE ExpenseCategory = '01250A30-3865-40D2-9E49-DA2B9898B1F4') AS TotalMileage,
(SELECT SUM(TotalAmount) FROM @temp WHERE ExpenseCategory = 'F4E4582D-DEDF-499D-BBC1-45003B865218') AS TotalEquip,
(SELECT SUM(TotalAmount) FROM @temp WHERE ExpenseCategory = '55F471C1-6E52-4898-8263-0B7684B78E7C') AS TotalPPS,
(SELECT SUM(TotalAmount) FROM @temp WHERE ExpenseCategory = '1EE0FC40-193F-4720-9143-0B61FFDE7B8D') AS TotalOther
FROM @temp GROUP BY EmployeeId

Can you try this?
Usually when Group By is used it should be followed with "having" at the end. It should not be present in where condition