存储过程基于多个条件和表连接对值求和

时间:2017-07-04 15:43:40

标签: sql sql-server stored-procedures

我正在尝试为以下数据集编写存储过程:

表A:

ReconID | service    | customer   | date
1       | Service A  | Customer A | July 2017
2       | Service A  | Customer A | July 2017
3       | Service A  | Customer B | July 2017
4       | Service A  | Customer B | Aug 2017

表B:

ReconID | actual_duration
1       | 10
2       | 20
3       | 5
4       | 2

我想:

1)加入两个表,

2)分组依据:服务,客户和月份+年份,然后将实际持续时间相加。

因此,在这种情况下,我的新表将具有以下内容:

service    | customer | date | sum_actual_duration
Service A  | Customer A | July 2017  | 30    *(First two records were summed to make 30)*
Service A  | Customer B | July 2017  | 5
Service A  | Customer B | Aug 2017   | 2

**尝试但未通过存储过程:( **

create PROCEDURE sum_ActualDuration
    @service varchar(50),
    @customer varchar(50),
    @date varchar(10),
    @sum_value int OUTPUT

SET @sum_value = (SELECT SUM( Z.actual_duration)
    FROM B
    LEFT OUTER JOIN A
    ON (A.ReconID = B.ReconID
WHERE A.service = @service AND A.customer = @customer AND A.date = @date
    )
AS
Begin
-- INSERT the new record
INSERT INTO sum_ActualDuration (service, customer, date, sum_value)
VALUES(@service, @customer, @date, @sum_value
    FROM B
    LEFT OUTER JOIN A
    ON (A.ReconID = B.ReconID)
    WHERE service = @service AND customer = @customer AND date = @date
)
end

非常感谢任何帮助!

---更新答案---

/* --- CREATE TABLE --- */

--DROP TABLE A;
--DROP TABLE B;

CREATE TABLE A(reconID integer, service text, customer text, date text);

INSERT INTO A VALUES(1, 'Service A', 'Customer A', 'Jul-17');
INSERT INTO A VALUES(2, 'Service A', 'Customer A', 'Jul-17');
INSERT INTO A VALUES(3, 'Service A', 'Customer B', 'Jul-17');
INSERT INTO A VALUES(4, 'Service A', 'Customer B', 'Aug-17');
COMMIT;

SELECT * FROM A;

CREATE TABLE B(reconID integer, actual_duration integer);

INSERT INTO B VALUES(1, '10');
INSERT INTO B VALUES(2, '20');
INSERT INTO B VALUES(3, '5');
INSERT INTO B VALUES(4, '2');
COMMIT;

SELECT * FROM B;

/* --- STORED PROCEDURE --- */

--CREATE PROC sp_actualDuration
--AS


--DROP TABLE sum_ActualDuration;
CREATE TABLE sum_ActualDuration(service text, customer text, date text, sum_value integer);

INSERT INTO sum_ActualDuration (service, customer, date, sum_value)
SELECT a.service, a.customer, a.date, SUM(b.actual_duration)
FROM A a LEFT OUTER JOIN
    B b
    ON a.ReconID = b.ReconID
    GROUP BY a.service, a.customer, a.date;
    --WHERE a.service="Service A" AND a.customer="Customer A" AND a.date="Jul-17";

--END

--EXEC sp_actualDuration

SELECT * FROM sum_ActualDuration;

2 个答案:

答案 0 :(得分:1)

您可以使用group by和sum,如下所示:

Select B.Service, B.Customer, B.[Date], Sum(A.Actual_duration)
FROM B
LEFT OUTER JOIN A
ON (A.ReconID = B.ReconID)
group by B.Service, B.Customer, B.[Date]

答案 1 :(得分:1)

我认为你可以在一个声明中完成这一切,INSERT . . . SELECT

INSERT INTO sum_ActualDuration (service, customer, date, sum_value)
    SELECT a.service, a.customer, a.date, SUM(b.actual_duration)
    FROM A a LEFT OUTER JOIN
         B b
         ON a.ReconID = b.ReconID
    WHERE a.service = @service AND a.customer = @customer AND a.date = @date;

您似乎希望A表成为外连接的第一个表。另请注意,我对所有列名称进行了限定。