追加新数据并通过插入另一个数据更新表中的现有数据
在 SQL 中,我有一个包含 3 列的表,该表每天都会刷新。我想将“表a”数据附加到“表b”。我需要确保按日期没有重复的行,而且销售计数会增加,销售计数会更新,而不是放入新行。
表 a
日期 | id | sale_count |
---|---|---|
12/20/21 | 1 | 7 |
12/20/21 | 2 | 3 |
12/21/21 | 2 | 2 |
表 b 预更新
日期 | id | sale_count |
---|---|---|
12/20/21 | 1 | 5 |
12/20/21 | 2 | 3 |
表 b发布更新
日期 | ID | sale_count |
---|---|---|
12/20/21 | 1 | 7 |
12/20/21 | 2 | 3 |
12/21/21 | 2 | 2 |
到目前为止我的代码仍然允许重复行
INSERT INTO tableB
SELECT
a.[date]
,a.id
,a.sale_count
FROM tableA a
Left outer join tableB b
on a.[date] = b.[date]
and a.id = b.id
and a.sale_count = b.sale_count
where b.[date] IS NULL
AND b.sale_count is NULL
In SQL, I have a table with 3 columns that is refreshed through the day. I want to append "table a" data to "table b". I need to ensure the there are no duplicate rows by date, but also that is the sale_count in creases that the sale count is updated and not put in a a new row.
table a
date | id | sale_count |
---|---|---|
12/20/21 | 1 | 7 |
12/20/21 | 2 | 3 |
12/21/21 | 2 | 2 |
table b pre-update
date | id | sale_count |
---|---|---|
12/20/21 | 1 | 5 |
12/20/21 | 2 | 3 |
table b post update
date | id | sale_count |
---|---|---|
12/20/21 | 1 | 7 |
12/20/21 | 2 | 3 |
12/21/21 | 2 | 2 |
code I have so far that is still allowing duplicate rows
INSERT INTO tableB
SELECT
a.[date]
,a.id
,a.sale_count
FROM tableA a
Left outer join tableB b
on a.[date] = b.[date]
and a.id = b.id
and a.sale_count = b.sale_count
where b.[date] IS NULL
AND b.sale_count is NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论