Hi smile,
Please refer below query.
SQL
DECLARE @A AS TABLE(ID INT,Price INT,Date VARCHAR(10))
INSERT INTO @A VALUES(1,100,'01.06.2023')
INSERT INTO @A VALUES(2,150,'01.06.2023')
INSERT INTO @A VALUES(3,250,'02.06.2023')
DECLARE @B AS TABLE(ID INT,Price INT,Date VARCHAR(10))
INSERT INTO @B VALUES(1,150,'01.06.2023')
INSERT INTO @B VALUES(2,150,'01.06.2023')
INSERT INTO @B VALUES(3,250,'02.06.2023')
DECLARE @C AS TABLE(ID INT,Price INT,Date VARCHAR(10))
INSERT INTO @C VALUES(1,250,'01.06.2023')
INSERT INTO @C VALUES(2,150,'01.06.2023')
INSERT INTO @C VALUES(3,250,'02.06.2023')
DECLARE @D AS TABLE(ID INT,Price INT,Date VARCHAR(10))
INSERT INTO @D VALUES(1,300,'01.06.2023')
INSERT INTO @D VALUES(2,150,'01.06.2023')
INSERT INTO @D VALUES(3,250,'02.06.2023')
DECLARE @Income INT, @Expenses INT
SET @Income = ((SELECT SUM(Price) FROM @A) + (SELECT SUM(Price) FROM @B))
SET @Expenses = ((SELECT SUM(Price) FROM @C) + (SELECT SUM(Price) FROM @D))
SELECT @Income - @Expenses AS 'Profit'
Output
Profit
-300