Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 将两个记录合并为一个_Sql Server_Stored Procedures_Coalesce - Fatal编程技术网

Sql server 将两个记录合并为一个

Sql server 将两个记录合并为一个,sql-server,stored-procedures,coalesce,Sql Server,Stored Procedures,Coalesce,我有一个存储两个值的表;'每个客户的“总计”和“欠款”。数据通过两个文件上传到表中,一个文件显示“总计”,另一个文件显示“欠”。这意味着每个customerID有两条记录: customerID:--------Total:--------- Owing: 1234---------------- 1000----------NULL 1234-----------------NULL-----------200 我想编写一个将两条记录合并在一起的存储过程: customerID:---

我有一个存储两个值的表;'每个客户的“总计”和“欠款”。数据通过两个文件上传到表中,一个文件显示“总计”,另一个文件显示“欠”。这意味着每个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
有什么想法吗

除了COUNT之外,聚合函数忽略空值。总数的 函数经常与SELECT的GROUPBY子句一起使用 声明

因此,您不需要担心求和时的空值。下面将为您的合并记录提供帮助

试试这个:

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
想知道为什么不在第二次执行文件时使用
UPDATE

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.

…但请注意,您需要一些机制来确保行1和行2是唯一的。My WHERE子句是一个基于您提供的数据的示例。您需要对此进行调整,以便为您的业务规则添加更具体的内容。

这是否真的应该像它为customerId所做的那样,而不仅仅是选择它?@muhmud这是用于将记录合并在一起的
,或者我指的是
所需的输出
。现在看起来正确了,您以前对此进行过一些聚合计算。唯一的问题是数据每小时更新一次,因此总值/欠值可以每小时更改一次…因此我需要检查是否存在customerId,然后重写该值,不要将两个总值相加。您需要一种机制来确保可以唯一选择行1和行2,并且如果两个值不匹配但非空(即行1.total为1000,行2.total为2000),则需要一种机制来设置优先级。假设你有这个,我下面的答案就行了。
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
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.