Distribute table data to another table using SQL query

Viewed 28

Help on how to distribute Default Table into Transaction Table using SQL Server Query if month is not existing on the other side? Please refer on the sample tables below

Default Table

Month Value
Jan 0
Feb 0
Mar 0
Apr 0
May 0

Transaction Table

Month Sales
Jan 10
Feb 0
Apr 20

I want to achieve this below results.

Month Sales
Jan 10
Feb 0
Mar 0
Apr 20
May 0
1 Answers

You can use INSERT...WHERE NOT EXISTS

INSERT INTO Transaction (Month, Sales)
SELECT c.Month, d.Sales
FROM [Default] d
WHERE NOT EXISTS (SELECT 1
    FROM Transaction t
    WHERE t.Month = d.Month)
Related