如何获取列值的总和作为SQL结果集中的新列

时间:2018-08-15 09:57:30

标签: sql sql-server

我有一个称为付款计划的表,我需要获取下表中的结果集。

CREATE TABLE PaymentPlans
(
PaymentPlanID           int, 
EmployeeID              int,        
PaidToDate              money
)

INSERT INTO PaymentPlans VALUES (1,1,100)
INSERT INTO PaymentPlans VALUES (2,1,200)
INSERT INTO PaymentPlans VALUES (3,1,150)

在我的选择查询中,我需要一个额外的列,该列为我提供“ PaidToDate”的总和。我正在尝试以下查询,在这里我得到的是“已付款总额”的结果集,与“迄今为止付款”相同。

SELECT PaymentPlanID, EmployeeID, PaidToDate, SUM(PaidToDate) AS 'TOTAL AMOUNT PAID' FROM PaymentPlans group by PaymentPlanID, EmployeeID, PaidToDate

SELECT a.PaymentPlanID, a.EmployeeID, a.PaidToDate, SUM(a.PaidToDate) AS 
'TOTAL AMOUNT PAID' FROM (
SELECT PaymentPlanID, EmployeeID, PaidToDate FROM PaymentPlans
)a
GROUP BY a.PaymentPlanID, a.EmployeeID, a.PaidToDate

在这两种情况下,我得到的结果如下,

PaymentPlanID   EmployeeID  PaidToDate  TotalAmountPaid
1               1           100.00      100.00
2               1           200.00      200.00
3               1           150.00      150.00

在我需要以下结果的地方,

PaymentPlanID EmployeeID    PaidToDate    TotalAmountPaid
1             1             100.00        450.00
2             1             200.00        450.00
3             1             150.00        450.00

请让我知道我的查询中缺少什么。

5 个答案:

答案 0 :(得分:2)

您可以尝试以下方式

select *,(select sum(PaidToDate) from  PaymentPlans) as total from PaymentPlans 

PaymentPlanID   EmployeeID  PaidToDate  total
1                  1         100.00    450.00
2                  1         200.00    450.00
3                  1        150.00     450.00

答案 1 :(得分:2)

您可以使用OVER子句。根据您的要求,您可能需要包含PARTITION BY子句。

SELECT PaymentPlanID,
       EmployeeID,
       PaidToDate,
       SUM(PaidToDate) OVER () AS TotalAmountPaid
FROM dbo.PaymentPlans;

答案 2 :(得分:2)

如果您有多个客户ID,并且希望每一行都具有该客户的总付款额,则需要按以下方式加入子查询

SELECT 
   a.PaymentPlanID, 
   a.EmployeeID, 
   a.PaidToDate, 
   b.TotalPaidToDate AS 'TOTAL AMOUNT PAID' 
FROM PaymentPlans a
INNER JOIN ( 
    SELECT 
       EmployeeID, 
       SUM(PaidToDate) AS TotalPaidToDate 
    FROM PaymentPlans 
    GROUP BY EmployeeID
) b
ON a.EmployeeID = b.EmployeeID

答案 3 :(得分:1)

我会使用Over子句,如果基础记录更多,该子句可能会很快执行。

SELECT 
      PaymentPlanID,EmployeeID,
      PaidToDate,SUM(PaidToDate) OVER () as total
FROM PaymentPlans

答案 4 :(得分:1)

要完成与Larnu的争论,

方法1:

SELECT PaymentPlanID,
       EmployeeID,
       PaidToDate,
       SUM(PaidToDate) OVER (partition by EmployeeID order by PaymentPlanID) AS TotalAmountPaidByEmployeeID,
       SUM(PaidToDate) OVER () AS TotalAmountPaid
FROM dbo.PaymentPlans;

方法2:

with Total as 
(
select EmployeeID, sum(PaidToDate) TotalAmountPaidByEmployeeID
from dbo.PaymentPlans
group by EmployeeID
)
SELECT f1.*, f2.TotalAmountPaidByEmployeeID
FROM dbo.PaymentPlans f1 inner join Total f2 on f1.EmployeeID=f2.EmployeeID;

方法3

SELECT f1.*, f3.TotalAmountPaidByEmployeeID
FROM dbo.PaymentPlans f1
cross apply
(
  select sum(PaidToDate) TotalAmountPaidByEmployeeID from dbo.PaymentPlans f2
  where f1.EmployeeID=f2.EmployeeID
) f3