2017-07-04 72 views
0

我试图写一个存储过程为以下组数据:存储过程基于多个条件和一个表来和值加入

表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; 

回答

1

您可以通过使用组和如下总结:

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

我认为你可以做到这一切在一个声明中,一个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表是外连接的第一个表。另请注意,我限定了所有列名。

相关问题