SQL-添加到现有行

SQL-添加到现有行,sql,sql-server,count,sum,Sql,Sql Server,Count,Sum,我有一个不断增长的表,我想在其中更新数据以进行总体计数。当更新发生时,插入的总计数数据可能已经包含在表中,并且还会添加新行。我不确定如何添加到前面的行,如果它们是新的,如何创建它们,如果它们已经存在,如何添加总和。例如 column1|column2|count --------------------- A |A |5 A |B |7 B |A |4 我将从另一个表中添加以下数据,但第1/2列将始终更改: {To be Added}

我有一个不断增长的表,我想在其中更新数据以进行总体计数。当更新发生时,插入的总计数数据可能已经包含在表中,并且还会添加新行。我不确定如何添加到前面的行,如果它们是新的,如何创建它们,如果它们已经存在,如何添加总和。例如

column1|column2|count
---------------------
A      |A      |5
A      |B      |7
B      |A      |4
我将从另一个表中添加以下数据,但第1/2列将始终更改:

{To be Added}
column1|column2|count
---------------------
A      |A      |10
B      |A      |2
C      |A      |5
因此,最终的表格如下所示:

column1|column2|count
--------------------
A      |A      |15
A      |B      |7
B      |A      |9
C      |A      |5
仔细研究这一点,我找到了使用
SUM()
的解决方案,但由于专栏总是在变化,这就是我遇到问题的地方。另外,创建一个暂存表以作为这些数据进行协作,而不是尝试在一个查询中更新这些数据,这会更容易吗?
感谢您的帮助,
非常感谢,

Jordan

是的,您可以为此使用
MERGE
语句:

Merge Into Table As Target 
Using (Select Column1, Column2, Count From OtherTable) As Source 
    On (Target.Column1 = Source.Column1 And Target.Column2 = Source.Column2) 
When Matched Then
    Update Set Count = Target.Count + Source.Count
When Not Matched Then
    Insert (Column1, Column2, Count) 
    Values (Source.Column1, Source.Column2, Source.Count)
;

这将根据列1和列2的值将数据从其他表合并到主表中。如果该值不存在,它将插入记录。否则,它将
用两个值之和更新表。

是的,您可以使用
合并
语句:

Merge Into Table As Target 
Using (Select Column1, Column2, Count From OtherTable) As Source 
    On (Target.Column1 = Source.Column1 And Target.Column2 = Source.Column2) 
When Matched Then
    Update Set Count = Target.Count + Source.Count
When Not Matched Then
    Insert (Column1, Column2, Count) 
    Values (Source.Column1, Source.Column2, Source.Count)
;

这将根据列1和列2的值将数据从其他表合并到主表中。如果该值不存在,它将插入记录。否则,它将
用两个值的总和更新表。

您也可以在这些查询的帮助下以这种方式进行更新

--first update common rows
update t1
set t1.count=t1.count+t2.counts
select * from tbl1 t1 inner join 
(select column1,column2,SUm(count) as counts from tbl2 
group by column1,column2)
t2 on t1.column1=t2.column1 and t1.column2=t2.column2

--now insert remaining rows
insert into t1(column1,column2,count) 
select t2.column1,t2.column2,t2.counts as count 
from tbl1 t1 right join 
   (select column1,column2,SUm(count) as counts from tbl2 
    group by column1,column2) t2
 on t1.column1=t2.column1 and t1.column2=t2.column2
where t1.column1 is NULL

在这些查询的帮助下,您也可以这样做

--first update common rows
update t1
set t1.count=t1.count+t2.counts
select * from tbl1 t1 inner join 
(select column1,column2,SUm(count) as counts from tbl2 
group by column1,column2)
t2 on t1.column1=t2.column1 and t1.column2=t2.column2

--now insert remaining rows
insert into t1(column1,column2,count) 
select t2.column1,t2.column2,t2.counts as count 
from tbl1 t1 right join 
   (select column1,column2,SUm(count) as counts from tbl2 
    group by column1,column2) t2
 on t1.column1=t2.column1 and t1.column2=t2.column2
where t1.column1 is NULL

C | A | 9
行从何而来?如果不知道列值的历史记录并将该历史记录与当前值关联,就没有你想要的总结方法。对不起,是的,你的权利。写这篇文章时,我的数学很差。更新了问题。感谢您指出这一点。这表示日志数据,column1类似于它的名称,而Column2是一个子类别。计数是每一个动作发生的时间。总体结果是创建使用中产品的简单分析视图。
C | a | 9
行从何而来?如果不知道列值的历史记录并将该历史记录与当前值关联,就无法像您所希望的那样进行总结。对不起,是的,您的权利。写这篇文章时,我的数学很差。更新了问题。感谢您指出这一点。这表示日志数据,column1类似于它的名称,而Column2是一个子类别。计数是每一个动作发生的时间。总体结果是创建使用中产品的简单分析视图。