Hi usernewman6,
Please refer below query.
CREATE TABLE #table1 (
Id INT
,Name VARCHAR(50)
,CreatedOn DATE
,SpentAmt INT
)
INSERT INTO #table1 VALUES (1,'abc','2023/03/31','1000')
INSERT INTO #table1 VALUES (2,'Test','2023/03/31','14000')
INSERT INTO #table1 VALUES (3,'Mark','2023/03/31','2000')
INSERT INTO #table1 VALUES (4,'Robert','2023/03/31','700')
CREATE TABLE #table2 (
Id INT
,Name VARCHAR(50)
,CreatedOn DATE
,UsedAmt INT
)
INSERT INTO #table2 VALUES (1,'abc','2023/04/01','2000')
INSERT INTO #table2 VALUES (7,'Grace','2023/03/23','4000')
INSERT INTO #table2 VALUES (2,'Test','2023/03/15','10000')
INSERT INTO #table2 VALUES (9,'Mary','2023/03/10','1000')
SELECT
COALESCE(t1.Id,t2.Id)Id,
COALESCE(t1.Name,t2.Name)Name,
t1.SpentAmt,
t2.UsedAmt
FROM #table1 t1
FULL JOIN #table2 t2
ON t1.Id = t2.Id;
DROP TABLE #table1
DROP TABLE #table2
Output
Id |
Name |
SpentAmt |
UsedAmt |
1 |
abc |
1000 |
2000 |
2 |
Test |
14000 |
10000 |
3 |
Mark |
2000 |
NULL |
4 |
Robert |
700 |
NULL |
7 |
Grace |
NULL |
4000 |
9 |
Mary |
NULL |
1000 |