2013-02-20 93 views
0

我有一个存储两个值的表;为每个客户提供“总计”和“欠款”。数据上传到表格使用两个文件,一个带来'总',另一个带来'欠'。这意味着我有两个记录每个的customerID:将两条记录合并为一条

customerID:--------Total:--------- Owing: 

1234---------------- 1000----------NULL 

1234-----------------NULL-----------200 

我想写一个存储过程,这两个记录合并在一起:

customerID:--------Total:--------- Owing: 

1234---------------- 1000----------200 

我一直在使用COALESCE等摆放在一起像这样的例子可见:

BEGIN 

-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
SET NOCOUNT ON; 

--Variable declarations 

DECLARE @customer_id varchar(20) 
DECLARE @total decimal(15,8) 
DECLARE @owing decimal(15,8) 
DECLARE @customer_name_date varchar(255) 
DECLARE @organisation varchar(4) 
DECLARE @country_code varchar(2) 
DECLARE @created_date datetime 

--Other Variables 
DECLARE @totals_staging_id int 

--Get the id of the first row in the staging table 
SELECT @totals_staging_id = MIN(totals_staging_id) 
from TOTALS_STAGING 

--iterate through the staging table 
WHILE @totals_staging_id is not null 
BEGIN 

update TOTALS_STAGING 

SET 
total = coalesce(@total, total), 
owing = coalesce(@owing, owing) 

where totals_staging_id = @totals_staging_id 

END 
END 

任何想法?

+0

您需要一种机制来确保row1和row2可以被唯一选择,并且如果两个值不匹配但是非空(即row1.Total为1000和row2),您将需要一种机制来设置优先级。总数是2000)。假设你有这个,我的答案会起作用。 – 2013-07-20 22:31:50

回答

0

除COUNT之外,聚合函数忽略空值。集合 函数经常与SELECT 语句的GROUP BY子句一起使用。 MSDN

所以你不需要担心与求和的空值。以下将给你的合并记录在一起Fiddle-demo

select customerId, 
     sum(Total) Total, 
     sum(Owing) Owing 
from T 
Group by customerId 
+0

应该真的在为customerId做些什么,而不是仅仅选择它? – muhmud 2013-02-20 10:47:07

+0

@muhmud这是为了'合并记录',或者我的意思是'想要的输出'。 – Kaf 2013-02-20 10:59:03

+0

现在看起来是正确的,你之前有过一些总计算。 – muhmud 2013-02-20 14:31:31

0

试试这个:

CREATE TABLE #Temp 
(
    CustomerId int, 
    Total int, 
    Owing int 
) 

insert into #Temp 
values (1024,100,null),(1024,null,200),(1025,10,null) 



Create Table #Final 
(
    CustomerId int, 
    Total int, 
    Owing int 
) 

insert into #Final 
values (1025,100,50) 



MERGE #Final AS F 
USING 
(SELECT customerid,sum(Total) Total,sum(owing) owing FROM #Temp 
group by #Temp.customerid 
) AS a 

ON (F.customerid = a.customerid) 
WHEN MATCHED THEN UPDATE SET F.Total = F.Total + isnull(a.Total,0) 
           ,F.Owing = F.Owing + isnull(a.Owing,0) 
WHEN NOT MATCHED THEN 
INSERT (CustomerId,Total,Owing) 
VALUES (a.customerid,a.Total,a.owing); 

select * from #Final 

drop table #Temp 
drop table #Final 
+0

唯一的问题是,数据每小时更新一次,所以总值/负值可以每小时更改一次...... – user1571352 2013-02-20 11:06:57

+0

所以我需要检查是否存在客户ID是否存在,然后写入值,不要将两个总值相加在一起 – user1571352 2013-02-20 11:07:47

+0

@ user1571352:检查更新。 – 2013-02-21 05:33:21

1
SELECT t1.customerId, t1.total, t2.owing FROM test t1 JOIN test t2 ON (t1.customerId = t2.customerId) WHERE t1.total IS NOT NULL AND t2.owing IS NOT NULL 

你为什么不只是使用UPDATE上的第二文件执行想知道?

0

这应该工作:

SELECT CustomerID, 
     COALESCE(total1, total2) AS Total, 
     COALESCE(owing1, owing2) AS Owing 
FROM 
(SELECT row1.CustomerID AS CustomerID, 
     row1.Total AS total1, 
     row2.Total AS total2, 
     row1.Owing AS owing1, 
     row2.Owing AS owing2 
FROM YourTable row1 INNER JOIN YourTable row2 ON row1.CustomerID = row2.CustomerID 
WHERE row1.Total IS NULL AND row2.Total IS NOT NULL) temp 
--Note: Alter the WHERE clause as necessary to ensure row1 and row2 are unique. 

...但是请注意,你需要一些机制来保证ROW1和2行是唯一的。我的WHERE子句是基于您提供的数据的示例。您需要调整此设置以添加更具体的业务规则。