1 year ago
#336152
Shay D
Append new data and update exisitng data in table from inserting another
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
sql
sql-server
left-join
outer-join
isnull
0 Answers
Your Answer