【发布时间】:2019-01-22 04:59:35
【问题描述】:
我有一个称为付款计划的表格,我需要得到下表中的结果集。
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
请让我知道我的查询中缺少什么。
【问题讨论】:
-
在样本数据中添加另一个EmployeeID,并相应地调整预期结果。
-
在示例数据中添加另一个 EmployeeID !!!
标签: sql sql-server